Skip to content

Instantly share code, notes, and snippets.

@timReynolds
Created June 17, 2017 12:04
Show Gist options
  • Save timReynolds/3360580163e21d283052541f1c62e84c to your computer and use it in GitHub Desktop.
Save timReynolds/3360580163e21d283052541f1c62e84c to your computer and use it in GitHub Desktop.
Segment.com with next.js
<Head>
<script dangerouslySetInnerHTML={{ __html: `
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"];analytics.factory=function(t){return function(){var e=Array.prototype.slice.call(arguments);e.unshift(t);analytics.push(e);return analytics}};for(var t=0;t<analytics.methods.length;t++){var e=analytics.methods[t];analytics[e]=analytics.factory(e)}analytics.load=function(t){var e=document.createElement("script");e.type="text/javascript";e.async=!0;e.src=("https:"===document.location.protocol?"https://":"http://")+"cdn.segment.com/analytics.js/v1/"+t+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(e,n)};analytics.SNIPPET_VERSION="4.0.0";
analytics.load("t65z006w75");analytics.page();}
`}} />
</Head>
@archywillhe
Copy link

not really working in my case. I'm going for the official approach instead https://github.com/vercel/next.js/tree/canary/examples/with-segment-analytics

@timReynolds
Copy link
Author

Yeah this is a very dated solution and no longer best practise

@benjaminhoffman
Copy link

@archywillhe @timReynolds I'm not sure the with-segment-analytics example is best practices. I'm experimenting and looking deeper into this and determined to get this right (mostly because I used to work at Segment and currently implementing it in my nextjs app so take pride in ensuring it's correct and consistent for others to use).

For now, here is a detailed comment I left on an open issue about my findings: vercel/next.js#11535 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment