analytics icon indicating copy to clipboard operation
analytics copied to clipboard

How do you use 3rd-party analytics APIs?

Open matthew-dean opened this issue 2 years ago • 1 comments

Platforms like Gtag have an API that supports things like retrieving the generated clientId, which we need for passing the GA id to segment. The code looks something like:

  gtag('get', GA_ID, 'client_id', clientId => {
      gaClientId = clientId;
      sendClientToSegment();
  });

However, I don't see anything in Analytics or the Gtag plugin that supports this. Is that true?

matthew-dean avatar Jan 30 '23 17:01 matthew-dean

I haven't tried it yet, but I see you can pass in an object as the plugin config here: https://github.com/DavidWells/analytics/blob/master/packages/analytics-plugin-google-analytics/src/browser.js#L73-L79 and that gets passed to gtag as window[initConfig.gtagName]('config', tagId, settings)

So theoretically you could do this:

gtag('config', 'GA_ID', {
    'client_id': 'client_id',
    'session_id': 'session_id'
});

as:

const gaTracker = new Analytics({
    app: "app-name",
    plugins: [
        googleAnalytics({
            enabled: true,
            measurementIds: ["GA_ID"],
            config: {
                client_id: "client_id",
                session_id: "session_id",
            },
        }),
    ],
});

rushi avatar May 23 '25 07:05 rushi