Embedding Google Analytics - good or evil - poll

Thanks Boss! Based on your suggestion I ended up doing this:

  pageAnalytics = {
    const id = 'UA-XXXXXX-XX';
    await require(`https://www.googletagmanager.com/gtag/js?id=${id}`).catch(() => {});
    const dataLayer = window.dataLayer = window.dataLayer || [];
    gtag('js', new Date());
    gtag('config', id);
    return md`This page uses ***Google Analytics***, see: [Terms of Service](https://www.google.com/analytics/terms/)`;
    function gtag() { dataLayer.push(arguments); }
  }

Looks like all pages are reported as ‘/worker/worker.7352fb1a7c…53e60b20e6e912a2599e.html’ not sure yet how to fix this.
Perhaps this could be built into Observable, so that users just need to provide their id?

1 Like