Proper way to use Google Analytics inside Content Scripts

I want to set up the Google Analytics on my new web extension the right way as described in @jorgev’s blog post (https://blog.mozilla.org/addons/2016/05/31/using-google-analytics-in-extensions/).

I’m confused about few things:

  • Is the GA_CLIENT_ID same as the client id mentioned here - https://developers.google.com/analytics/devguides/collection/analyticsjs/cookies-user-id#getting_the_client_id_from_the_cookie

  • If so, doesn’t the client_id change every time you relaunch Firefox (or the browser)? From my tests, looks like a new client_id is generated based on the code mentioned in the Google’s documentation above.

  • This is the architecture of my web extension - It’s has a content script that creates an iframe and all the data & UI are displayed inside that iframe. I want to include Google Analytics inside that iframe. If I use ga.js to inject the Google Analytics script inside the iframe - is that an acceptable design or is that incompatible with your review policies?

I worry that if we assign a static client_id (like @jorgev did in his example), it won’t be possible to determine how many number of users are using the extension - since client_id is used to determine unique users.

I just want to know what’s the right way to implement this in a way that respects Firefox policies, respects the users’ privacy & also helps me as a developer determine the usage of my web extension.

It looks like the client_id should be unique per installation, meaning it should be unique per user. I don’t remember if I used a constant just because it was an example or I just didn’t read the docs right.

As for your other question, you should use the API instead of injecting the script, when possible. If the script is injected in a content-only scope, it may be allowed, but it’s a harder determination to make and could lead to your add-on being rejected or taking a long time to be reviewed.

Thanks for the clarification regarding both the questions.

Looks like according to Google Analytics developer guide, client_id is a randomly generated GUID which I can generate within my add-on.

So I ended up adapting your example with a randomly generated UUID as client_id and using that instead of injecting Google Analytics script into my content script’s iframe.

I had the same confusion. Every example I can find on how to get the client ID uses the remote script implementation, so I’m not sure how to generate unique client ids using the implementation outlined in @jorgev 's blog.
Is there a tutorial or something that you could share? Or maybe even the code you used to implement this?