SPA link_click tracking

Hi, I have a SPA client with backbone.js, I realize link_click event is not being track for all my links, I check and you are adding event listeners to links in the list document.links, which does not have all the links added with js. I would like to know what would be the less invasive way to start tracking all my link clicks.

Thanks.

@jose, the links (<a href=...>) will be tracked automatically if they are present in DOM once the tracker has loaded (and enableLinkClickTracking is initiated). Any other autogenerated links added later on would have to be engaged by calling the refreshLinkClickTracking method after the link was added to DOM: https://github.com/snowplow/snowplow/wiki/2-Specific-event-tracking-with-the-Javascript-tracker#392-refreshlinkclicktracking. Also note the other HTML elements used in link capacity (ex. <button>) will not be tracked without a “manual” tracking with trackLinkClick.

Hi where should I put the refreshLinkClickTracking?..

I assume I should put it in my Pageview tag in GTM.

So it looks like this?

> <script type="text/javascript">
> ;(function(p,l,o,w,i,n,g){if(!p[i]){p.GlobalSnowplowNamespace=p.GlobalSnowplowNamespace||[];
> p.GlobalSnowplowNamespace.push(i);p[i]=function(){(p[i].q=p[i].q||[]).push(arguments)
> };p[i].q=p[i].q||[];n=l.createElement(o);g=l.getElementsByTagName(o)[0];n.async=1;
> n.src=w;g.parentNode.insertBefore(n,g)}}(window,document,"script","//d1fc8wv8zag5ca.cloudfront.net/2.5.1/sp.js","snowplow"));
> 
> window.snowplow('newTracker', 'cf', '//sp.xxxxxx.com', { // Initialise a tracker
>   appId: 'Elevenia',
>   //cookieDomain: null,
>   discoverRootDomain: true,
>   forceUnsecureTracker: true,
>   stateStorageStrategy: "localStorage",
>   contexts: {
>     webPage: true,
>     performanceTiming: false,
>     gaCookies: false,
>     geolocation: false
>   }
> });
> 
> window.snowplow('setUserId', {{User ID}});
> window.snowplow('enableActivityTracking', 30, 10);
> window.snowplow('trackPageView');
> window.snowplow('enableLinkClickTracking');
> window.snowplow('refreshLinkClickTracking');
> </script>

link_click event don’t always get triggered on my website for some reason, I suspect it’s because some of the links get generated late. I want to test whether refreshLinkClickTracking fix that.

Thanks.

@gagabonar, you need to be certain that the <a> tag is present in the DOM before calling refreshLinkClickTracking method. That is if the link is generated automatically by some piece of code the refreshLinkClickTracking method should be a part of that code. Placing it immediately after enableLinkClickTracking is not likely to make any difference.

got it. Thanks!

Just to provide some closure to my earlier question.

I ended up not using the enableLinkClickTracking function, and used the manual trackLinkClick instead by putting it inside a GTM tag that was created specifically for firing any link clicks from my website. The GTM already have this ‘Just Links’ trigger type out of the box, so I used that to trigger the tag.

So far, I’ve been able to capture all my links using this approach. as explained by @ihor , Snowplow enableLinkClickTracking is initialized together with the page view, at this point some links may not be finished being generated yet. My guess is GTM ‘Just Links’ trigger initialize the Link Click object at that point when the link click event happen (which at that point the href links have finished being constructed). CMIIW.

Thanks.