React-ga: Using more than one tracking code

Created on 19 Aug 2015  路  7Comments  路  Source: react-ga/react-ga

Hey,

I want to use react-ga with more than one tracking code.

Is it possible? Should I initialize react-ga again to switch between tracking codes? Can I create multiple instances of react-ga to manage different Google Analytics accounts?

Thanks!

Most helpful comment

I was able to add a second account by using ReactGA.ga() directly:

import ReactGA from 'react-ga';
ReactGA.initialize('UA-000000-1');
ReactGA.ga('create', 'UA-000000-2', 'auto', {'name':'myOtherTracker'});

const logPageView = () => {
    ReactGA.set({ page: window.location.pathname });
    ReactGA.pageview(window.location.pathname);
    ReactGA.ga('myOtherTracker.send', 'pageview', {'page': window.location.pathname});
    return null;
};

See https://developers.google.com/analytics/devguides/collection/analyticsjs/creating-trackers for more on creating trackers

See https://developers.google.com/analytics/devguides/collection/analyticsjs/command-queue-reference#send for more on the ga.send() method

All 7 comments

Hi!

I guess you've done something about it.
Here's a documentation about using multiple trackers on a single page.
But it's not possible with the current version of react-ga, because it doesn't pass options.

That's awesome @mrsln, thank you!

I'm assuming I need to access the original ga function in order to support multiple tracking tags here, or is there a cleaner way to do it here.

@reywright I haven't used the library for a long time but I think you can try something like this:

ReactGA.initialize('UA-000000-01', {
  gaOptions: {
    name: 'myTracker1',
  },
});

ReactGA.initialize('UA-000000-01', {
  gaOptions: {
    name: 'myTracker2',
  },
});

@reywright I am having the same issue. I wonder how you solved it? Did you use a second instance as @mrsln suggested?

I ended up using vanilla GA as I just didn't trust this method, and I needed to use GA on some of our applications non React pages. So I put GA on the same page I mount my React stuff, then I just referenced that in my react components or action files. Eslint complained but I added GA to it so it wouldn't complain anymore.

I was able to add a second account by using ReactGA.ga() directly:

import ReactGA from 'react-ga';
ReactGA.initialize('UA-000000-1');
ReactGA.ga('create', 'UA-000000-2', 'auto', {'name':'myOtherTracker'});

const logPageView = () => {
    ReactGA.set({ page: window.location.pathname });
    ReactGA.pageview(window.location.pathname);
    ReactGA.ga('myOtherTracker.send', 'pageview', {'page': window.location.pathname});
    return null;
};

See https://developers.google.com/analytics/devguides/collection/analyticsjs/creating-trackers for more on creating trackers

See https://developers.google.com/analytics/devguides/collection/analyticsjs/command-queue-reference#send for more on the ga.send() method

Was this page helpful?
0 / 5 - 0 ratings

Related issues

olalonde picture olalonde  路  3Comments

Dimomir picture Dimomir  路  3Comments

hlodver picture hlodver  路  4Comments

astroana picture astroana  路  5Comments

horaceleung picture horaceleung  路  6Comments