Strange JS error with javascript tracker with multiple sp.js load

Hi there!

I have a strange situation. I am using sp.js (v2.7.0) for tracking page_views, page_pings and a few self-describing events. My site is also loading an external js plugin called keywee.js (that basically scans through the content). This plugin is also making its own call to sp.js since it also uses sp.js (v2.4.3) for its own internal tracking of our websites. So we now have 2 instances of the sp.js tracker (v2.7 and v.2.4.3) loading on our site.

Page loads and pings fire properly. However, when I try to fire the unstruct event (on a mouseclick), the other sp.js throws an error.

Uncaught TypeError: Cannot read property 'apply' of undefined
at Object.u [as push] (http://dc8xl0ndzn2cb.cloudfront.net/sp.js:31:324)
....
                    for (x = 0; x < C.length; x++) {
                        C[x][A].apply(C[x], z)
                    }
....

The error above is referencing the spjs loaded through keywee inclusion and not my sp.min.js here. Even though I created the new tracker and snowplow in the head as async addition as per below:

<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","/mediav2/build/assets/js/sp.min.js","Sp"));
window.Sp('newTracker', 'scala', '<collector-url>', {
              appId: '<web-id>',
              platform: "web"
            });
window.Sp('enableActivityTracking', 30, 10);
window.Sp('trackPageView');
</script>

The events thats thowing the error is structured as:

      window.ihSp('trackSelfDescribingEvent', {
      schema: 'iglu:com.site.iglu/site_event/jsonschema/1-0-0',
      data: {
        actionType: 'open',
        shareUrl: encodeURI(this.data),
        senderEmail: this.email
      }
  });

Its difficult to trace this error precisely but I was wondering if there’s a way to handle multiple sp.js loads for different trackers on the same page without conflicts like the above?

One thing I noted is that this error is not consistently reproducible. I happens on some page loads, which leads me to think it may be something related to load sequence (which js loads first?). But this is not clear…However once the error occurs, its breaks the js on the website completely.

Thanks very much!

Hello @kjain,

Thanks for raising this. You’re second user who experience this weird issue. In another case, 3rd party who installed second tracker was also keywee (also 2.4.3). However in that case our user received little amounts of events sent by keywee tracker.

I created a ticket to explore it more closely: https://github.com/snowplow/snowplow-javascript-tracker/issues/560

Do you always receive this traceback on trackSelfDescribingEvent or very rarely (which would signal a race condition)? If you have any public page where I can reproduce it - a link (as well as other debug info) would be highly appreciated.

Thanks for the update Anton!

It appears that when I initialize my custom event using the ‘trackSelfDescribingEvent’ method, the call loads the keywee sp.js (2.4.3) instead of my version of the js (2.7.0). And I am pretty sure that ‘trackSelfDescribingEvent’ doesn’t exist in that version (you were using a different event unstruct earlier). Which is why the error is thrown. But of course, this doesn’t explain why is keywee’s sp.js being referred to here, especially since both are using different namespaces.

I think I have found a workaround for this for now by loading my sp.js within a limited scope (I use require.js for managing all the js handlers) and that seems to help (still testing though). But it would be great to get to the bottom of this issue with multiple trackers.

Thanks!