google-analyticshyperlinkonclickevent-trackingaffiliate

Tracking user link clicks with google analytics


I have created an affiliate marketing website. Here users will register on my website and shop, however the shopping link will be of amazon.

I need to know how to set up Google Analytics so that it can track which registered user has clicked an affiliate link on my website.

For example I have this amazon link https://www.amazon.in/BassHeads-225-Super-Extra-Headphones/dp/B01M9C51T9/ref=sr_1_1?_encoding=UTF8&pf_rd_i=desktop&pf_rd_m=A1VBAL9TL5WCBF&pf_rd_p=14ffe21a-e5a2-45c9-a9c0-91f47b082bbc&pf_rd_r=94G5Y8CM93G8M5Y2SNS3&pf_rd_t=36701&qid=1554008093&s=gateway&smid=A14CZOWI0VEHLG&sr=8-1

I know I'm supposed to put an onClick event on there somewhere but I don't have any idea how it links to Google Analytics? I am using Google Tag Manager

Is this the correct Onclick code:

onClick="_gaq.push(['_trackEvent', 'Link', 'Click', 'Banner Advert1']);"

If not what do I need to add to track the registered user?


Solution

  • I have many links to external sites on my website; an example link looks like this:

    <a id="Buy" data-itemDescription="(a description)" data-itemValue="2.80" href=http://externalsite?id=6789>
    

    The key things here are the extra attributes before the href: id (used to identify the specific event that occurs when the link is clicked, i.e. in this case a Buy event) and the data-itemDescription and data-itemValue metadata (used in constructing the event label etc.)

    The relevant GTM artefacts are as follows:

    Trigger:

    Buy: Click - Just Links when Click Id contains Buy

    User-defined variables (custom Javascript):

    ItemDescription:

    function() {
      return {{Click Element}}.getAttribute("data-itemDescription");
      }
    

    ItemValue:

    function() {
      return {{Click Element}}.getAttribute("data-itemValue");
      }
    

    ItemValueFloat (100 multiplier used because GA didn't like my decimal places - I then divide by 100 at reporting time to get the right answer):

    function() {
      return parseFloat({{ItemValue}})*100;
      }
    

    Tag BuyClicked is an Event triggered by the Buy trigger as above, with:

    Category = Purchase Tracking
    
    Action = Purchase {{ItemDescription}}
    
    Label = {{Page Path}} : {{Click URL}}
    
    Value = {{ItemValueFloat}}
    

    I also have Non-Interaction Hit set True.

    The BuyClicked event is then collected by GA, and reportable on from e.g. Google Data Studio, without my having to do anything further.

    If you can't add an id or metadata, you could undoubtedly do the same sort of thing more painfully by handling all clicks through a single trigger and then parsing the Click URL via custom Javascript to get granular Categories, Actions, Labels.