centre-for-effective-altruism / givingwhatwecan-org-static

Static build for Giving What We Can (givingwhatwecan.org) using Metalsmith and Netlify
5 stars 0 forks source link

Analytics stopped working since june 18/ 19 #79

Closed jasperhartong closed 3 years ago

jasperhartong commented 3 years ago

When loading https://www.givingwhatwecan.org/ an error is logged in the console: Uncaught TypeError: Cannot read property '_writeKey' of undefined. This error comes from the analytics.js of Segment, pulled in from https://cdn.segment.com/analytics.js/v1/VtTlzxOC5QJ1H0zermvo84cAbReTofDU/analytics.min.js.

Looking at the Segment analytics documentation, it seems that v1 is now considered "Classic" and v2 is the current version we should use.

Actually, Segment might have performed an automatic migration on June 15 according to this upgrade manual, according to Segment "it should just work" after the update.

Just checked the incoming analytics.js: it does mention a version number of 1.24.6, which makes me believe it's actually not or not completely upgraded.

NB: I don't have access to Segment at the moment to check out more

jasperhartong commented 3 years ago

Also just found a similar issue, seems we need to update the way we include Segment:

https://github.com/josemarluedke/ember-cli-segment/pull/92/files

jasperhartong commented 3 years ago

Yes indeed, running the following seems to fix the analytics (events are starting to be send out again and report "success"):

$("body").append(`<script type="text/javascript">
          !function(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","debug","page","once","off","on","addSourceMiddleware","addIntegrationMiddleware","setAnonymousId","addDestinationMiddleware"];analytics.factory=function(e){return function(){var t=Array.prototype.slice.call(arguments);t.unshift(e);analytics.push(t);return analytics}};for(var e=0;e<analytics.methods.length;e++){var key=analytics.methods[e];analytics[key]=analytics.factory(key)}analytics.load=function(key,e){var t=document.createElement("script");t.type="text/javascript";t.async=!0;t.src="//cdn.segment.com/analytics.js/v1/" + key + "/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(t,n);analytics._loadOptions=e};analytics._writeKey="VtTlzxOC5QJ1H0zermvo84cAbReTofDU";analytics.SNIPPET_VERSION="4.13.2";
          analytics.load("VtTlzxOC5QJ1H0zermvo84cAbReTofDU");
          }}();
        </script>`)