You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Apr 28, 2022. It is now read-only.
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.jsvarloadAnalyticsJs=function(){varanalytics=analytics||[];analytics.load=function(e){vart=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";varn=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(t,n);varr=function(e){returnfunction(){analytics.push([e].concat(Array.prototype.slice.call(arguments,0)));};},s=["identify","track"];for(vari=0;i<s.length;i++)analytics[s[i]]=r(s[i]);},analytics.load("XXXXXXX");};// Example of usage that causes errorsetTimeout(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?
The text was updated successfully, but these errors were encountered:
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.
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.When this is used, I see this error in the console:
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?
The text was updated successfully, but these errors were encountered: