DavidWells / analytics

Lightweight analytics abstraction layer for tracking page views, custom events, & identifying visitors
https://getanalytics.io
MIT License
2.43k stars 247 forks source link

HTML integration does not recognize Analytics.page analytics.track #431

Closed daneFFW closed 4 months ago

daneFFW commented 5 months ago

I tried using the HTML demo here https://analytics-html-example.netlify.app/ but get errors in the console

(index):57 Uncaught TypeError: Cannot read properties of undefined (reading 'page') at pageView ((index):57:18) at HTMLButtonElement.onclick ((index):40:34)

24-04-26-1053

I am also getting similar errors on my test site running the html version. https://verceltest-iota-jet.vercel.app/index-DW.html

24-04-26-1055

DavidWells commented 5 months ago

That demo is pretty old running the now dead google analytics v3.

Try with the latest version of Google analytics (totally different product sigh)

DavidWells commented 5 months ago

Actually, https://unpkg.com/@analytics/google-analytics/dist/@analytics/google-analytics.min.js is new GA4.

Demo is outdated. Try the below code

<script src="https://unpkg.com/analytics/dist/analytics.min.js"></script>
<script src="https://unpkg.com/@analytics/google-analytics/dist/@analytics/google-analytics.min.js"></script>
<script type="text/javascript">
      /* Initialize analytics */
      var Analytics = _analytics.init({
        app: 'analytics-html-demo',
        debug: true,
        plugins: [
          // attach google analytics plugin
          analyticsGa({
            measurementIds: ['G-abc123']
          })
          // ... add any other third party analytics plugins
        ]
      })
      /* Fire page view */
      Analytics.page()
      /* Attaching a listener */
      Analytics.on('*', ({ payload }) => {
        console.log(`Event ${payload.type}`, payload)
      })
</script>
daneFFW commented 4 months ago

@DavidWells Thanks!! Ill check it outl.