Skip to content
This repository has been archived by the owner on Apr 28, 2022. It is now read-only.

Error on loading analytics.js asynchronously after page load #39

Closed
robbles opened this issue Jan 6, 2013 · 2 comments
Closed

Error on loading analytics.js asynchronously after page load #39

robbles opened this issue Jan 6, 2013 · 2 comments

Comments

@robbles
Copy link

robbles commented Jan 6, 2013

I'm seeing an error when trying to load analytics.js later on, after the page has loaded. I have a function, loadAnalyticsJs which is called later on in the page execution, since we only want to track a small subset of page views.

// Function to lazy-load analytics.js
var loadAnalyticsJs = function() {
  var analytics=analytics||[];analytics.load=function(e){var t=document.createElement("script");t.type="text/javascript",t.async=!0,t.src=("https:"===document.location.protocol?"https://":"http://")+"d2dq2ahtl5zl1z.cloudfront.net"+"/analytics.js/v1/"+e+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(t,n);var r=function(e){return function(){analytics.push([e].concat(Array.prototype.slice.call(arguments,0)));};},s=["identify","track"];for(var i=0;i<s.length;i++)analytics[s[i]]=r(s[i]);},
  analytics.load("XXXXXXX");
};

// Example of usage that causes error
setTimeout(loadAnalyticsJs, 10000);

When this is used, I see this error in the console:

Uncaught TypeError: Cannot read property 'length' of undefined analytics.min.js:4

I haven't had the chance to look through the un-minified source for analytics.js yet, but I'm guessing this has something to do with inspecting the script tags on the page, and that analytics.js is assuming the presence of a script tag that loaded it?

@robbles
Copy link
Author

robbles commented Jan 6, 2013

Problem solved - the line that throws the exception was looking for window.analytics, which of course is not defined by that snippet, because it assumes it's running in the global scope.

I changed it to the following code and now analytics.js loads without errors. However, I think that the loading snippet should be changed to accommodate loading from a function, since it's a small fix.

var loadAnalyticsJs = function() {
  var analytics=analytics||[];analytics.load=function(e){var t=document.createElement("script");t.type="text/javascript",t.async=!0,t.src=("https:"===document.location.protocol?"https://":"http://")+"d2dq2ahtl5zl1z.cloudfront.net"+"/analytics.js/v1/"+e+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(t,n);var r=function(e){return function(){analytics.push([e].concat(Array.prototype.slice.call(arguments,0)));};},s=["identify","track"];for(var i=0;i<s.length;i++)analytics[s[i]]=r(s[i]);},
  window.analytics = analytics;
  analytics.load("XXXXXXX");
};

@ianstormtaylor
Copy link
Contributor

Hey @robbles I'm gonna close this for now, but keep attaching to window in mind in case it comes up again.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants