-
Exploring GA4 Measurement Protocol Integration for Chrome Extensions
Hey all, I’m trying to figure something out and could do with a little help. I’m attempting to add GA4 analytics to a Chrome Extension through the Measurement Protocol (gtag).
I’ve been using this StackOverflow post as a guide, but it only covers Universal Analytics and not GA4, which uses a different protocol. Bit of a headache.
The exact code from the google instructions has been popped into the service worker, which seemed to check out in the Event Builder.
const measurement_id =
G-XXXXXXXXXX
; const api_secret =<secret_value>
; fetch(https://www.google-analytics.com/mp/collect?measurement_id=${measurement_id}&api_secret=${api_secret}
, { method: "POST", body: JSON.stringify({ client_id: 'XXXXXXXXXX.YYYYYYYYYY', events: [{ name: 'tutorial_begin', params: {}, }] }) });But for some reason, I’m not getting any event data through to GA. The extension console seems to be error-free, just no data rolling in.
Journey Update:
Got some progress here! Managed to get it working by moving the const values straight into the fetch URL:
fetch(
https://www.google-analytics.com/mp/collect?measurement_id=G-XXXXXXXXXX&api_secret=secret_value
, { method: "POST", body: JSON.stringify({ client_id: 'XXXXXXXXXX.YYYYYYYYYY', events: [{ name: 'tutorial_begin', params: {}, }] }) });
Log in to reply.