Ad

How To Use React.GA.plugin.require With Multiple Trackers

- 1 answer

With the package react-ga, how to require the plugin ecommerce with multiple trackers ?

There is my working initialization code :

const initTracker = (trackerId, name) => ({
  trackingId: trackerId,
  gaOptions: { name }
});

const initializeGA = () => {
  const trackers = [initTracker("UA-XXXXXXXX-1", "trackerGlobal")];
  if (window.ga) {
    const trackingId = ga.getAll()[0].get("trackingId");
    trackers.push(initTracker(trackingId, "trackerSite"));
  }
  ReactGA.initialize(trackers, {
    debug: true,
    alwaysSendToDefaultTracker: false
  });
  ReactGA.plugin.require("trackerGlobal.ecommerce"); //Don't work
  ReactGA.plugin.require("trackerSite.ecommerce"); // Don't work
  return trackers;
};

And how to execute an addTransaction on a specific tracker ?

ReactGA.plugin.execute(`ecommerce`, `addTransaction`, {
  id: orderId,
  affiliation: "Shop",
  revenue: price
});

Thanks,

Ad

Answer

Ok, I find a way to implement it :

To require the ecommerce plugin :

ReactGA.ga("trackerGlobal.require", "ecommerce");

To add a transaction:

ReactGA.ga(`${trackerName}.ecommerce:addTransaction`, {
  id: orderId,
  affiliation: "Affiliation",
  revenue: order.price
});
Ad
source: stackoverflow.com
Ad