javascripturlgoogle-analyticshyperlinkbit.ly

Get the bit.ly link on the page to which bit.ly link redirects using JavaScript


How could I get the bit.ly link on the page to which bit.ly link redirects using JavaScript?

I need this for tracking purposes. I have a few bit.ly links and I would like to track how many times each one of them was clicked in GA.

Thank you.


Solution

  • You could do the following if you insist on working with Javascript.

    Send the bit.ly link as a GET parameter with the destination URL. For example: Change this

    <a href="receiver.html">bit.ly/link_1</a>
    

    to

    <a href="receiver.html?sending_url="bit.ly/link_1">bit.ly/link_1</a> 
    

    Then in receiver.html use

    location.search 
    

    property to get the list of parameters. Parse this to extract the url that was clicked. One way to do so is to use regular expressions in Javascript. This tutorial is a good starting point https://www.w3schools.com/jsref/jsref_obj_regexp.asp

    However, the above technique is a hack and not the correct way to send and receive data across pages. Javascript is a client-side language. A much easier and appropriate way would be to use a server-side language such as PHP to process the sent data.