79325326

Date: 2025-01-03 01:57:55
Score: 0.5
Natty:
Report link

I ended up adding something like the following to index.html for conditional tagging based on hostname. Replace the gTagDev and gTagProd (aka Measurement ID from google analytics data stream) and YOUR_DEV_URL (dev.ourdomain.com) if needed.

<script>
  // DEV vs PROD data stream
  // inspired by https://medium.com/@danielfilipkowskiblogs/adding-google-analytics-to-angular-v15-1766c4bdaed5
  const gtagDev = 'G-123456789'
  const gtagProd = 'G-987654321'
  const gtagID = /YOUR_DEV_URL|localhost/.test(window.location.hostname) ? gtagDev : gtagProd

  // from https://stackoverflow.com/questions/13121948/dynamically-add-script-tag-with-src-that-may-include-document-write
  var googleTagManager = document.createElement('script');
  googleTagManager.setAttribute('src', `https://www.googletagmanager.com/gtag/js?id=${gtagID}`);
  document.head.appendChild(googleTagManager);

  window.dataLayer = window.dataLayer || [];
  function gtag(){ dataLayer.push(arguments) }
  gtag('js', new Date());
  gtag('config', gtagID);
</script>
Reasons:
  • Blacklisted phrase (0.5): medium.com
  • Blacklisted phrase (1): stackoverflow
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: corg