Manifest v3 - Using gatag.js inside popup

135 views
Skip to first unread message

Sudhakar B

unread,
Jun 14, 2023, 9:57:59 PM6/14/23
to Chromium Extensions
Hi Everyone,

I have included gatag.js in the extension and invoking the gatag function in the popup, its not triggering any events. I see window.dataLayer is getting populated with the events, but not triggering.

Any hints on getting it working ?

Sample code

function gtag () 
{ dataLayer.push(arguments) 
 gtag('js', new Date()) 
 gtag('config', 'G-XXXXX', { debug_mode: true })


David Vallejo

unread,
Jun 15, 2023, 3:22:19 AM6/15/23
to Sudhakar B, Chromium Extensions
GA4 (gtag.js) won't work if the protocol is different from http/https , it will just aborts the execution.

You may try this: https://ga4mp.dev to track your extension. Please have in mind m the lib doesn't take care of the session state, so you should take care of that yourself ( ie: writing a cookie/store, with the clientId, sessionId, session count )

In the other side it will only add 4kb-ish.



--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/59cf6930-cc7f-416a-ae7b-e10eeab4f1c9n%40chromium.org.

Sudhakar B

unread,
Jun 15, 2023, 3:48:08 AM6/15/23
to Chromium Extensions, David Vallejo, Chromium Extensions, Sudhakar B
Ok, Let me try. 

Thank you. 

Sebastian Benz

unread,
Jun 19, 2023, 9:37:32 AM6/19/23
to Sudhakar B, Chromium Extensions, David Vallejo
We've also published a new guide explaining how to use GA4 in a Chrome Extension together with a sample extension

Dr. Sebastian Benz

Developer Programs Engineer

sb...@google.com


Google Germany GmbH

Erika-Mann-Straße 33

80636 München


Geschäftsführer: Paul Manicle, Liana Sebastian

Registergericht und -nummer: Hamburg, HRB 86891

Sitz der Gesellschaft: Hamburg


Diese E-Mail ist vertraulich. Falls Sie diese fälschlicherweise erhalten haben sollten, leiten Sie diese bitte nicht an jemand anderes weiter, löschen Sie alle Kopien und Anhänge davon und lassen Sie mich bitte wissen, dass die E-Mail an die falsche Person gesendet wurde. 

     

This e-mail is confidential. If you received this communication by mistake, please don't forward it to anyone else, please erase all copies and attachments, and please let me know that it has gone to the wrong person.


Sudhakar B

unread,
Jun 19, 2023, 12:47:12 PM6/19/23
to Chromium Extensions, Sebastian Benz, Chromium Extensions, David Vallejo, Sudhakar B
Thank you, yes for now I'm using the measurement protocol api. I see right now geolocation information will not be included, any timeline when it might be available ?

David Vallejo

unread,
Jun 19, 2023, 1:07:24 PM6/19/23
to Sudhakar B, Chromium Extensions, Sebastian Benz
I really think that repo should be removed, It's a terrible idea having your API_SECRET exposed to everyone within the extension. The GA4 measurement protocol is not meant to do a full tracking but for augmenting the current session data. You won't be able to measure sessions/users ( first_visit, session_start, events ), because that's not supported by the Measurement Protocol.  


--

Sebastian Benz

unread,
Jun 19, 2023, 4:16:38 PM6/19/23
to David Vallejo, Sudhakar B, Chromium Extensions
I agree that sharing the API key is not ideal and if this is a concern you can proxy requests through your own server (we should update our docs to mention this - thanks for the feedback). 

Measurement protocol is currently the only officially supported way to use GA4 in Chrome Extensions. Sessions and user id are both supported. Geo location is not supported out of the box, if you require this you need to add this information via a custom event property. 

David Vallejo

unread,
Jun 19, 2023, 4:27:14 PM6/19/23
to Sebastian Benz, Sudhakar B, Chromium Extensions
Sebastian even if you proxy the hits it won't be able to properly set the attribution, session_start events, etc. 

I hate spamming, but my library supports geo location, session stuff, in-build events, client-hints, and everything on gtag.js but without all the cookies and Google Signals related stuff. It's just 4kb and you can use it as a module, or umd on any project. I know that is not anything official, 
but it relies on the official Client-Side protocol used by the GTAG library, which is pretty safe, and my help on people needs. Just saying

On the other side, even Google Support docs actively advise about the MP to be used only to augment the data. Even if you're not exposing the API Client through a proxy, you will be hitting bogus metrics. 
I think Google should just add the possibility to skip the protocol check and to skip the cookie storage availability ( ie:  checkProtocolTask and storage:"none" in GA3) ,which was reporte 2.5 years ago without any proper progress/update: https://issuetracker.google.com/issues/174954288?pli=1
--


Reply all
Reply to author
Forward
0 new messages