DavidWells / DavidWells/analytics

useAnalytics.page() doesn't send an event type to GTM

Open
#148 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
2.7k
Forks
267
PR merge metrics
No merged PRs in 30d

Description

I'm trying to track page views in my React app by using the hook provided by this package. Here's my basic setup:

import * as React from 'react'
import Analytics from 'analytics'
import {AnalyticsProvider as UseAnalyticsProvider} from 'use-analytics'
import googleTagManager from '@analytics/google-tag-manager'

const init = Analytics({
    app: 'sense-dashboard',
    plugins: [
        googleTagManager({
            containerId: GTAG_CONTAINER_ID,
        }),
    ],
})

const AnalyticsProvider = ({children}: Children) => {
    return (
        <UseAnalyticsProvider instance={init}>
            <App />
        </UseAnalyticsProvider>
    )
}

const App = () => {
    const location = useLocation()
    const {page} = useAnalytics()

    React.useEffect(() => {
        page()
    }, [location, page])

    ...
}

This doesn't seem to register page views in Google Tag Manager, because no event type is sent with the page() call. If I change it to this:

const App = () => {
    const location = useLocation()
    const {page} = useAnalytics()

    React.useEffect(() => {
        page({event: 'Pageview'})
    }, [location, page])

    ...
}

Then everything works as expected. It took a lot of digging to uncover this issue though, and I definitely expected this to be the default behavior. Is this intended behavior? Is there something I'm missing in my set up that would have made this work?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the useAnalytics.page() entry point and the @analytics/google-tag-manager integration, comparing page() with page({event: 'Pageview'}). Reproduce the example and verify that a default page-view event type reaches Google Tag Manager without requiring the caller to provide event data.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
analytics, frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.