envoyproxy / envoy-mobile

Client HTTP and networking library based on the Envoy project for iOS, Android, and more.
https://envoymobile.io
Apache License 2.0
557 stars 84 forks source link

Enable Google Analytics 4 (GA4) #2680

Closed chalin closed 1 year ago

chalin commented 1 year ago

As discussed elsewhere (https://cncfservicedesk.atlassian.net/browse/CNCFSD-1458), the plan is to enable GA4 by using the existing GA4 Envoy web data stream with ID G-DXJEH1ZRXX.

For some context, the main repo was migrated to GA4 via:

Let's use this issue to discuss implementation details.

Tasks:

chalin commented 1 year ago

From @jpsim:

The Envoy Mobile website is hosted by GitHub Pages here: GitHub - envoy-mobile/envoy-mobile.github.io: Envoy Proxy website

The site is auto-generated using Sphinx whenever there are changes to the Envoy Mobile repo: envoy-mobile/docs at main · envoyproxy/envoy-mobile

I was able to hook up Google Analytics in Sphinx using this extension: GitHub - sphinx-contrib/googleanalytics

However, it seems to expect a GA tag with the format UA-123-123-123, will that work with the G-DXJEH1ZRXX tag that you shared earlier?

I'm not familiar with that extension so I don't know if it supports GA4. We can give it a try :).

jpsim commented 1 year ago

Giving it a try here: https://github.com/envoyproxy/envoy-mobile/pull/2681

chalin commented 1 year ago

I'm not seeing any GA4 ID in the site pages, are you @jpsim? If not, can you re-open this issue :)?

chalin commented 1 year ago

Oh ... now that I've caught up with your comment in the Jira ticket, I see that what you've done is enabled GA over the doc pages. I was looking at the main site. Can you reopen this issue so that we can use it to track the addition of GA to the main site too?

jpsim commented 1 year ago

Since you have access to the analytics dashboard, can you check if you see traffic from the EM docs site which was deployed about an hour ago? Like on this page? https://envoymobile.io/docs/envoy-mobile/latest/index.html

chalin commented 1 year ago

AFAIK, the GA4 console doesn't, at the moment, have as good realtime traffic data as UA does, so I can't tell if there's traffic from the EM docs site yet. Usually, I test pages in true realtime using the Google Tag Assistant, but it is refusing to connect to the site, apparently because the homepage doesn't have a GA ID.

@jpsim - can you add the GA4 ID to all of the main (non-docs) site pages? Then I'll be able to use the tag assistant.

jpsim commented 1 year ago

Ok @chalin, I just updated the only other non-doc page in https://github.com/envoy-mobile/envoy-mobile.github.io/pull/27

jpsim commented 1 year ago

Seems to be hooked up correctly on the website side of things. Here's the network activity when loading https://envoymobile.io before and after https://github.com/envoy-mobile/envoy-mobile.github.io/pull/27:

Before

before

After

after

chalin commented 1 year ago

From the analytics console I'm seeing traffic only from the landing page:

image

Your "after" network screenshot above confirms that the sphinxcontrib.googleanalytics plugin uses the old analytics.js library. GA4 requires the gtag.js library. Maybe you can propose an update to the plugin? (For details see, Migrate from analytics.js to gtag.js (Universal Analytics).)

chalin commented 1 year ago

I see that the sphinxcontrib.googleanalytics plugin was updated to support gtag.js in August: https://github.com/sphinx-contrib/googleanalytics/blob/42b3df99fdc01a136b9c575f3f251ae80cdfbe1d/sphinxcontrib/googleanalytics.py#L13

So maybe it's a question of fetching the latest version?

chalin commented 1 year ago

Here is what I used on another project:

sphinxcontrib-googleanalytics @ git+https://github.com/sphinx-contrib/googleanalytics.git@42b3df99fdc01a136b9c575f3f251ae80cdfbe1d
jpsim commented 1 year ago

We require tagged releases for our Python dependencies. Here's what I get when I point to the latest commit:

ERROR: Can't verify hashes for these requirements because we don't have a way to hash version control repositories:
    sphinxcontrib-googleanalytics@ git+https://github.com/sphinx-contrib/googleanalytics.git@42b3df99fdc01a136b9c575f3f251ae80cdfbe1d from git+https://github.com/sphinx-contrib/googleanalytics.git@42b3df99fdc01a136b9c575f3f251ae80cdfbe1d (from -r ./docs/requirements.txt (line 105))

I've requested a tagged release in https://github.com/sphinx-contrib/googleanalytics/issues/5

jpsim commented 1 year ago

Updating in https://github.com/envoyproxy/envoy-mobile/pull/2708