Has anyone successfully added GA4 analytics to a Chrome Extension using the Measurement Protocol (gtag).
This post has the details for Universal Analytics, but has not been updated for GA4 which has a different protocol.
I have copied the exact code from the google instructions into the service worker (also validated by 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: {},
}]
})
});
The extension console serves no errors, I just don't see any event data coming through to GA.
UPDATE
I managed to get it to work by moving the const values 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: {},
}]
})
});