Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to Google Analytics 4 #1839

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Update to Google Analytics 4 #1839

wants to merge 1 commit into from

Conversation

Nateowami
Copy link
Collaborator

@Nateowami Nateowami commented May 10, 2023

This change was copied verbatim from the Google Analytics interface.


This change is Reviewable

@codecov
Copy link

codecov bot commented May 10, 2023

Codecov Report

Patch coverage: 80.64% and project coverage change: +0.01 🎉

Comparison is base (6ab80a4) 67.81% compared to head (bd0b2b4) 67.82%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1839      +/-   ##
==========================================
+ Coverage   67.81%   67.82%   +0.01%     
==========================================
  Files         406      407       +1     
  Lines       24434    24459      +25     
  Branches     3681     3685       +4     
==========================================
+ Hits        16569    16589      +20     
- Misses       7304     7309       +5     
  Partials      561      561              
Impacted Files Coverage Δ
...cripture/ClientApp/src/environments/environment.ts 100.00% <ø> (ø)
...c/SIL.XForge.Scripture/Pages/Shared/_Layout.cshtml 0.00% <ø> (ø)
...Forge.Scripture/ClientApp/src/app/app.component.ts 81.22% <50.00%> (+0.07%) ⬆️
...e/ClientApp/src/xforge-common/analytics.service.ts 80.00% <80.00%> (ø)
...ntApp/src/xforge-common/error-reporting.service.ts 68.18% <100.00%> (-1.39%) ⬇️

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Collaborator

@nigel-wells nigel-wells left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

I assume you've also made the required adjustments inside Google Tag Manager to pass the data through to GA4?

Reviewed 1 of 1 files at r1, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @Nateowami)

@nigel-wells nigel-wells self-assigned this May 15, 2023
@Nateowami
Copy link
Collaborator Author

@nigel-wells I have never understood what Google Tag Manager is, despite seeing it referenced everywhere in relation to Google Analytics. I'm pretty sure it's configured correctly. Can we look at it together and make sure before merging?

nigel-wells
nigel-wells previously approved these changes May 15, 2023
Copy link
Collaborator

@nigel-wells nigel-wells left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've talked this over with Mieke, she says the code you have will send the data directly to Google Analytics without the need to setup anything specific inside of Google Tag Manager. You could proceed with what you have in this PR and it will work. What you would lose is the ability to compare data going into both Google Analytics 4 and Google Universal Analytics which you could do if you were using Google Tag Manager containers.

At some point it would be good to get a Google Tag Manager container setup as then other events and triggers could be setup for tracking specific things in the app. Did you want to go through the process of getting the base of that setup now as part of this change or do later? Mieke could certainly help with the setup if we can get permission on your account to manage it.

Happy to chat things through.

Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @Nateowami)

@Nateowami Nateowami force-pushed the update/ga-4 branch 6 times, most recently from 7c3f77b to bd0b2b4 Compare May 17, 2023 00:33
@Nateowami
Copy link
Collaborator Author

I somehow missed that we configure Google Analytics in a number of places. I've updated the other locations, and added logic to sanitize URLs before they're sent, as we've already been doing with Bugsnag.

@Nateowami Nateowami dismissed nigel-wells’s stale review May 17, 2023 19:03

PR has changed a ton, so dismissing review

@Nateowami Nateowami requested a review from nigel-wells May 18, 2023 21:07
Copy link
Collaborator

@nigel-wells nigel-wells left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 11 of 11 files at r2, all commit messages.
Reviewable status: all files reviewed, 10 unresolved discussions (waiting on @Nateowami)


src/SIL.XForge.Scripture/ClientApp/src/index.html line 4 at r2 (raw file):

<html lang="en">
  <head>
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-SVKBDV7K3Q"></script>

You'll want to remove the tag from here - I understand it can be set later using the config command.


src/SIL.XForge.Scripture/ClientApp/src/index.html line 10 at r2 (raw file):

        dataLayer.push(arguments);
      }
      gtag("js", new Date());

Apparently this may also trigger a page view and should be reserved for being set when you're ready to trigger something along with the config.


src/SIL.XForge.Scripture/ClientApp/src/index.html line 12 at r2 (raw file):

      gtag("js", new Date());

      gtag("config", "G-SVKBDV7K3Q");

You will want to remove this as this as the ID needs to come from the environment config plus I "think" it will trigger a page view. Essentially you want this specific config command to only be called once but I suspect, with the minimal tag manager configuration, it will also automatically trigger a page view.Bas


src/SIL.XForge.Scripture/ClientApp/src/app/app.component.ts line 150 at r2 (raw file):

      );
      this.subscribe(navEndEvent$, e => {
        if (this.isAppOnline) {

This check is already part of the new AnalyticsService so no need to have it here


src/SIL.XForge.Scripture/ClientApp/src/xforge-common/analytics.service.ts line 18 at r2 (raw file):

})
export class AnalyticsService {
  constructor(private readonly pwaService: PwaService) {}

Based on comments for index.html you may need to change part of this to ensure the config is set (and may the date?) but only on first logging. I'm not 100% (due to GTM setup) if setting that will in itself trigger a page view meaning you may not then need to trigger the alternative page_view event.


src/SIL.XForge.Scripture/ClientApp/src/xforge-common/analytics.service.ts line 27 at r2 (raw file):

  logNavigation(url: string): void {
    const sanitizedUrl = sanitizeUrl(url);
    this.logEvent('page_view', { page_path: sanitizedUrl });

The command is event and the type is page_view. You can then set params as seen here: https://developers.google.com/tag-platform/gtagjs/reference/events#page_view


src/SIL.XForge.Scripture/ClientApp/src/xforge-common/analytics.service.ts line 30 at r2 (raw file):

  }

  private logEvent(eventName: string, eventParams: EventParams): void {

I'd stay away from event unless you're dealing specifically with a Google Analytics "event". Google's docs refers to the first param as a "command" followed up with the tag and additional parameters. We may want to specifically trigger a real "event" at some point in the future - so I'd prefer we reserved wording for that situation.


src/SIL.XForge.Scripture/ClientApp/src/xforge-common/analytics.service.ts line 40 at r2 (raw file):

// redact access token from the hash
function redactAccessToken(url: string): string {

Perhaps we should put all these methods in its own utils file. Perhaps have a service we can call with one method and multiple ENUM options to pass with a string as to what needs to be redacted? This would save us having to remember all the different method names. It may not be obvious to look for bugsnag redact code in the analytics service.


src/SIL.XForge.Scripture/ClientApp/src/xforge-common/analytics.service.spec.ts line 10 at r2 (raw file):

  it('should redact the join key from URL', () => {
    const url = 'https://example.com/join/123';

Also run a test (or change to) for URLs that have the language code at the end i.e. /join/123/en


src/SIL.XForge.Scripture/Pages/Shared/_Layout.cshtml line 18 at r2 (raw file):

            gtag('js', new Date());

            gtag('config', 'G-SVKBDV7K3Q');

Can this come from an environment variable as well?

Copy link
Collaborator

@nigel-wells nigel-wells left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you want to pick this one back up again @Nateowami ? It would be good to get it across as GA4 is well underway now and I'm not sure how much longer before UA is dropped.

Reviewable status: all files reviewed, 10 unresolved discussions (waiting on @Nateowami)

Copy link
Collaborator Author

@Nateowami Nateowami left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be great. Thanks.

And coming back to Reviewable, I see I had a draft response that may still be relevant:

I think I finally figured out where the gtag documentation is: https://developers.google.com/analytics/devguides/collection/ga4?hl=en

Reviewable status: all files reviewed, 10 unresolved discussions (waiting on @nigel-wells)


src/SIL.XForge.Scripture/ClientApp/src/xforge-common/analytics.service.ts line 30 at r2 (raw file):

Previously, nigel-wells (Nigel Wells) wrote…

I'd stay away from event unless you're dealing specifically with a Google Analytics "event". Google's docs refers to the first param as a "command" followed up with the tag and additional parameters. We may want to specifically trigger a real "event" at some point in the future - so I'd prefer we reserved wording for that situation.

Ah, didn't know that. So maybe use the "command" terminology?


src/SIL.XForge.Scripture/Pages/Shared/_Layout.cshtml line 18 at r2 (raw file):

Previously, nigel-wells (Nigel Wells) wrote…

Can this come from an environment variable as well?

Probably, but it didn't look like it would be trivial.

Copy link
Collaborator

@nigel-wells nigel-wells left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you be happy for me to run through making these updates as you're going to be out of action for a bit? You can then review. I'll just stick to what we have here rather than introducing any other special events or tracking as that can be added later on.

Reviewable status: all files reviewed, 10 unresolved discussions (waiting on @Nateowami)

@Nateowami
Copy link
Collaborator Author

Yes, that would be great.

@nigel-wells
Copy link
Collaborator

I have this mostly working now with data flowing through to GA4. It is only triggering, however, on first page load. I suspect this is a setting that needs to be tweaked in the Google Tag Manager instance to work better with SPA instances. Am I able to get permission to view the settings in there as well please?

@codecov-commenter
Copy link

codecov-commenter commented Feb 7, 2024

Codecov Report

Attention: Patch coverage is 80.00000% with 6 lines in your changes missing coverage. Please review.

Project coverage is 78.31%. Comparing base (2671953) to head (ab04cca).
Report is 1 commits behind head on master.

Files Patch % Lines
...e/ClientApp/src/xforge-common/analytics.service.ts 80.00% 5 Missing ⚠️
...Forge.Scripture/ClientApp/src/app/app.component.ts 50.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1839   +/-   ##
=======================================
  Coverage   78.31%   78.31%           
=======================================
  Files         517      518    +1     
  Lines       29710    29735   +25     
  Branches     4849     4831   -18     
=======================================
+ Hits        23268    23288   +20     
- Misses       5704     5709    +5     
  Partials      738      738           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@nigel-wells
Copy link
Collaborator

I've taken another look at this and I see I can access the Google Tag Manager screen for the account which I didn't think I had access to. I'll take another look at this setup and will come back to you with next steps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants