analytics
analytics copied to clipboard
How do you use 3rd-party analytics APIs?
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?
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",
},
}),
],
});