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.

Is there a way to get page data inside onRouteUpdate (improving gatsby-plugin-google-analytics)

See original GitHub issue

The gatsby-plugin-google-analytics plugin implements onRouteUpdate method where it defines variables and sends data to Google Analytics:

var onRouteUpdate = function onRouteUpdate(_ref, pluginOptions) {
  ...
  var sendPageView = function sendPageView() {
    var pagePath = location ? location.pathname + location.search + location.hash : undefined;
    window.ga("set", "page", pagePath);
    window.ga("send", "pageview");
  };
  ...
};

I want to improve it and add the ability to set custom variables,

ga('set', 'dimension1', author.name );

This is important because using custom variables I can configure use custom dimensions in Google Analytics, like authors.

But I need to get access to page data to retrieve author’s name. How can I do that?

Once I solve this problem for myself, I’ll submit a PR. Thanks!

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
Js-Brechtcommented, Apr 14, 2020
// gatsby-browser.js
const doStuff = ({ location, pageContext, data }) => {
    console.log("location:", location);
    console.log("pageContext:", pageContext);
    console.log("data:", data);
}

export const wrapPageElement = ({ element, location, props }) => {
    doStuff({ location, ...props});
    return element
}

This works for me.

the wrapper component is not re-initialized when I navigate to other post from the current one

It is correct that it is not re-initialized, but it is, ultimately, a part of the React tree. When its props change, it will receive them, and when you navigate is when it receives new props.

If you wanted, you could even use a React component, so that you could use hooks inside of it.

// gatsby-browser.js
import React, { useEffect } from 'react';

const FooComponent = ({ location, pageContext, data }) => {
    useEffect(() => {
        console.log("location:", location);
        console.log("pageContext:", pageContext);
        console.log("data:", data);
    })
    return null;
}

export const wrapPageElement = ({ element, location, props }) => (
    <React.Fragment>
        <FooComponent  location={location} {...props} />
        {element}
    </React.Fragment>
)
1reaction
Js-Brechtcommented, Apr 18, 2020

Sure it’s no problem. Just make sure to use relative paths, and resolve them to absolute, that way it is portable.

You could use this to pass the path to the plugin:

{
  resolve: “foo-plugin”,
  options: { 
     fooApi: require.resolve(‘./src/utils/someModule’)
  }
}

And in your plugin, something like this:

export const onRouteUpdate = ({ location }, pluginOptions) => {
  const fooApiFn = require(pluginOptions.fooApi).default
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use Gatsby's Script API with Google Analytics
In this post I'll be explaining how to add Google's "new" Google Analytics Property (GA4) to your Gatsby Site using the new Gatsby...
Read more >
How to Add Google Analytics gtag to Gatsby Using Partytown
With Gatsby you will have probably used gatsby-plugin-google-analytics to add the Universal Analytics tracking code to your Gatsby site. In Part ...
Read more >
Gatsbyjs google analytics gdpr - Stack Overflow
I want to use google analytics in my website, but be gdpr compliant, so only fire it, when the user consents.
Read more >
Eight Time-Saving Gatsby Plugins - Better Programming
Gatsby is already easy to work with—now make it fast ... plugin provides drop-in support for server rendering data added with React Helmet, ......
Read more >
11 best GatsbyJS plugins for blog and website 2020 - Social9
How to use it: In your gatsby-config.js``module.exports plugins, you can have multiple instances of this plugin to read source nodes from ...
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