question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Set GA tracking ID outside of Nuxt Config somehow?

See original GitHub issue

It would be great if there was a way to add the tracking IDs into this module somehow. Currently I use nuxtServerInit to fetch them via Apollo add them into Vuex, but I can’t get them into Nuxt Config obviously.

Because the tracking codes are stored in our CMS, and we like clients to be able to change them without requiring a code change from us.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
drewbakercommented, Jun 12, 2019

For future reference, this is what I ended up with and it seems to work great. The only thing I’ not sure on is @dohomi has defer = true on the script, but google has async = true so I went with that instead.

export default ({ store, app: { router, context } }, inject) => {
    // Remove any empty tracking codes
    const codes = store.state.siteMeta.gaTrackingCodes.filter(Boolean)

    // Abort if no codes
    if (!codes.length) {
        if (context.isDev) console.log('No Google Anlaytics tracking codes set')
        return
    }

    // Abort if in Dev mode, but inject dummy functions so $gtag events don't throw errors
    if (context.isDev) {
        inject('gtag', () => {})
        return
    }

    // Check if we already added script to head
    let gtagScript = document.getElementById('gtag')
    if (gtagScript) {
        return
    }

    // Add script tag to head
    let script = document.createElement('script')
    script.async = true
    script.id = 'gtag'
    script.src = `https://www.googletagmanager.com/gtag/js?id=${codes[0]}`
    document.head.appendChild(script)    

    // Include Google gtag code and inject it (so this.$gtag works in pages/components)
    window.dataLayer = window.dataLayer || []
    function gtag() {
        dataLayer.push(arguments)
    }
    inject('gtag', gtag)
    gtag('js', new Date())

    // Add tracking codes from Vuex store
    codes.forEach(code => {
        gtag('config', code, {
            send_page_view: false // necessary to avoid duplicated page track on first page load
        })

        // After each router transition, log page event to Google for each code
        router.afterEach(to => {
            gtag('config', code, { page_path: to.fullPath })
        })
    })
}
0reactions
dohomicommented, Jun 8, 2019

I guess you are looking for something like this:

let script = document.createElement(`script`)
script.defer = true
script.onload = () => {
   // set a global var that your ga tag is loaded ?
}
script.src = `https://www.googletagmanager.com/gtag/js?id=${codes[0]}`
document.head.appendChild(script)

wrap this code into a function and just load it as long the script never been loaded before. I hope this leads you to the right way

Read more comments on GitHub >

github_iconTop Results From Across the Web

Set GA tracking ID outside of Nuxt Config somehow? #9 - GitHub
Currently I use nuxtServerInit to fetch them via Apollo add them into Vuex, but I can't get them into Nuxt Config obviously.
Read more >
Options - Google Analytics Module for Nuxt
The tracking ID of your Google Analytics account. It is required to have Google Analytics (GA) know which account and property to send...
Read more >
nuxtjs/google-tag-manager only add google analytics when ...
My website uses Nuxtjs and the @nuxtjs/google-tag-manager. In the nuxt-config.js I set it up like this modules: [ 'nuxt-leaflet', '@nuxtjs/ ...
Read more >
Add custom tracking events to your nuxt site with GA4
Tracking with GA4 can be very useful, here's a little guide on tracking things you find important with custom GA4 events in Nuxt....
Read more >
Track Single Page App with Google Analytics 4 and Google ...
Learn how to track Single Page Web App with Google Tag Manager (or Single Page Website), and send that pageview data to Google...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found