Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation for analytics pixels (such as Facebook or Reddit) #415

Open
kmjennison opened this issue Sep 17, 2019 · 0 comments
Open

Comments

@kmjennison
Copy link

Pre-rendering can break some analytics scripts, like the Reddit pixel, that assume their global is accessible before the external script loads.

Here's the Reddit pixel as an example:

<head>
  <script>
  !function(w,d){if(!w.rdt){var p=w.rdt=function(){p.sendEvent?p.sendEvent.apply(p,arguments):p.callQueue.push(arguments)};p.callQueue=[];var t=d.createElement("script");t.src="https://www.redditstatic.com/ads/pixel.js",t.async=!0;var s=d.getElementsByTagName("script")[0];s.parentNode.insertBefore(t,s)}}(window,document);rdt('init','abc123');
  </script>
</head>

After pre-rendering, when the script <script src="https://www.redditstatic.com/ads/pixel.js" async> loads before the inline code, it logs this error: "Reddit Pixel Error: Pixel was not initialized. Please ensure you have included the correct pixel script in your head tag"

Other pixels, like Facebook's, are structured similarly, though whether pre-rendering causes an error depends on the pixel impelementation.

The docs have info about Google Analytics, but I didn't see anything about inline analytics code like these.

The easiest solution, like Google Analytics, is to skip running the pixel code when pre-rendering:

<script>
if (navigator.userAgent !== 'ReactSnap') {
  // pixel code here
}
</script>

If this looks good, I'd be happy to send a PR with updated documentation.

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

No branches or pull requests

1 participant