gtag() compatibility #50
Replies: 8 comments
-
Hi @androidacy-user! Thanks for reaching out 👍 I would suggest this could be achieved by using a custom abstraction in your application. From memory, Something like this: import { track } from '@minimal-analytics/ga4';
/*[...]*/
const GA_ID = 'G-XXXXXXXX';
window.gtag = (method, ...args) => {
if(method === 'config' && args[0] === GA_ID) {
track(GA_ID) // page_view
return;
}
if(method === 'event') {
track({ type: args[0], event: args[1] }); // custom event
return;
}
/* etc, etc :) */
}; This would be a 1-1 proxy for triggering an event using Page View // maybe hold the measurement ID in a global var
gtag('config', 'G-XXXXXXXX'); Custom Event gtag('event', 'login', {'ep.user': 'John Smith'}); You'll need to cater to all uses of Ideally your application would use it's own analytics abstraction, so if you wanted to switch from say GA to something completely different, you'd just refactor the abstraction, not the usage. This of course, can be said for any third party integration. We won't be adding I hope this helps! Please let me know if you have any further questions, or require help with anything. It's important to note, if your application is not using a Node environment, we currently don't expose the All the best! 👍 |
Beta Was this translation helpful? Give feedback.
-
Its a browser based application for the most part - good ol' WordPress and/or Laravel on the backend @jahilldev |
Beta Was this translation helpful? Give feedback.
-
@androidacy-user In that case, if you're not using a build tool for your frontend, then for the above to work we'll need to expose the Once we've done that, something like I'll see if I can get a release done sometime this week for you. This has been on my watch list, but nobody thus far has piped up to say they want it, but now you have! Thanks again for contributing 👍 |
Beta Was this translation helpful? Give feedback.
-
@androidacy-user Ok I've opened a PR that introduces an additional config property, Once this is merged / deployed you'll have access to the It's off by default to avoid unnecessary Window pollution for those that don't want it. |
Beta Was this translation helpful? Give feedback.
-
Excellent, sounds great |
Beta Was this translation helpful? Give feedback.
-
@androidacy-user I've just deployed the latest version that provides the global function definition, v1.8.4. Let me know how you get on! |
Beta Was this translation helpful? Give feedback.
-
@androidacy-user Apologies, I've just identified a bug. Fixed now in v1.8.6! |
Beta Was this translation helpful? Give feedback.
-
For anyone who wants it, here's our generic gtag() wrapper around track() window.gtag = (event, eventName, data) => {
// For each element in the data object, add the key and value to the event data object. For strings, prepend ep. to the key, and for numbers, append epn. to the key.
for (const key in data) {
if (typeof data[key] === 'string') {
data[`ep.${key}`] = data[key]
delete data[key]
} else if (typeof data[key] === 'number') {
data[`epn.${key}`] = data[key]
delete data[key]
}
}
// Send the event to Google Analytics.
track('<tracking id>L', {
type: eventName,
event: {
...data
}
})
} |
Beta Was this translation helpful? Give feedback.
-
Hello!
We'd like to start using this library for GA4 on our site(s), but we're hitting a big snag right now: having to restructure all of our tracking calls to use mostly custom code.
Is there a plan to add gtag() or even dataLayer compatibility in the future?
Beta Was this translation helpful? Give feedback.
All reactions