Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gatsby-plugin-google-gtag window.gtag not working #12680

Closed
jsbrain opened this issue Mar 19, 2019 · 9 comments
Closed

gatsby-plugin-google-gtag window.gtag not working #12680

jsbrain opened this issue Mar 19, 2019 · 9 comments
Labels
status: needs more info Needs triaging and reproducible examples or more information to be resolved type: question or discussion Issue discussing or asking a question about Gatsby

Comments

@jsbrain
Copy link

jsbrain commented Mar 19, 2019

Hi, I'm trying to send a custom event with the gatsby-plugin-google-gtag plugin.

But window.gtag is not available. I really don't get it. What am I doing wrong?

Thanks

@jsbrain jsbrain changed the title gatsby-plugin-google-gtag| gatsby-plugin-google-gtag window.gtag not working Mar 19, 2019
@coreyward
Copy link
Contributor

@jsbrain Mind reader is busted today, can you manually provide details for debugging (I know, I know, it's archaic).

@coreyward coreyward added type: question or discussion Issue discussing or asking a question about Gatsby status: needs more info Needs triaging and reproducible examples or more information to be resolved labels Mar 19, 2019
@jsbrain
Copy link
Author

jsbrain commented Mar 20, 2019

@coreyward Yeah, that's what I thought reading the docs of the plugin.

The plugin says:

Custom Events
This plugin automatically sends a “pageview” event to all products given as “trackingIds” on every Gatsbys route change.

If you want to call a custom event you have access to window.gtag where you can call an event for all products:

window.gtag("event", "click", { ...data })
or you can target a specific product:

window.gtag("event", "click", { send_to: "AW-CONVERSION_ID", ...data })
In either case don’t forget to guard against SSR:

typeof window !== "undefined" && window.gtag("event", "click", { ...data })

And I am trying something like:

componentDidMount () {
  window.gtag('event', 'click', { ...data })
}

but it says that window.gtag is undefined. So obviously I'm misunderstanding something or the docs are misleading.

Thank you

@jsbrain
Copy link
Author

jsbrain commented Mar 20, 2019

Btw in case this question comes up: Yes, I tested in production ;)

@sidharthachatterjee
Copy link
Contributor

@jsbrain Just tried this out

Here's what I added to gatsby-config.js

{
  resolve: `gatsby-plugin-google-gtag`,
  options: {
    // You can add multiple tracking ids and a pageview event will be fired for all of them.
    trackingIds: [
      "UA-XXXXXXXX-X", // Google Analytics / GA
    ],
    pluginConfig: {
      // Puts tracking script in the head instead of the body
      head: true,
    },
  },
},

And I added a log in the index page component in the starter (this will be run after mount just like componentDidMount in your comment)

  useEffect(() => {
    console.log(window.gtag)
  }, [])

Screenshot 2019-03-20 16 39 10
And it seems to work like the screenshot above.

Can you link to your repository or try to make a minimal reproduction of this issue? That would help us track it down and help you resolve it.

@jcursoli
Copy link

jcursoli commented Mar 21, 2019

Hello I am experiencing the same issue, heres my config since i dont see anyone elses posted

    {
      resolve: 'gatsby-plugin-google-gtag',
      options: {
        trackingIds: ["UA-XXXXXXXX"],
        pluginConfig: {
          head: true,
          respectDNT: true,
        },
      },
    },

and im using the window.gtag like this
window.gtag('event', `blah blah`);

and im getting Uncaught TypeError: window.gtag is not a function

"gatsby": "2.0.50",
"gatsby-plugin-google-gtag": "^1.0.16",

NOTE: I also tried head: false and that did not work wither

@jsbrain
Copy link
Author

jsbrain commented Mar 21, 2019

Hi @sidharthachatterjee , thank you for your help.

I was able to resolve the issue. My browser cache was serving me an old version of my site 🤦‍♂️ So I removed all application data from chrome and voila it worked!

@jcursoli Make sure to run in production mode (eg. gatsby build && gatsby serve). It is not working with gatsby develop, as stated in the docs ;)

@jcursoli
Copy link

@jsbrain Yes i tried it in production, cleared cache etc.

@jcursoli
Copy link

Okay i think i got it, the problem is when you specify respectDNT option as true, it becomes undefined even though i dont have do not track options set on my browser. once i remove it, it works. But I still would like to have that option available. @sidharthachatterjee

@sidharthachatterjee
Copy link
Contributor

@jcursoli Just tested out setting respectDNT to true. It works fine when DNT is set to false in my browser. When setting DNT to true in Chrome browser settings, it becomes undefined (which is correct behaviour)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs more info Needs triaging and reproducible examples or more information to be resolved type: question or discussion Issue discussing or asking a question about Gatsby
Projects
None yet
Development

No branches or pull requests

4 participants