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

Google Tag Manager setup #34554

Merged
merged 10 commits into from
Jul 3, 2024
Merged

Google Tag Manager setup #34554

merged 10 commits into from
Jul 3, 2024

Conversation

ajeety4
Copy link
Contributor

@ajeety4 ajeety4 commented May 3, 2024

Product Description

Sets up Google Tag Manager at the HQ end along with logic to send user_id and related properies to GTM.

Technical Summary

Ticket is available here

Note that earlier way of installation of GTM script via base.html was replaced with loading it through javascript. This was
done to be in consistent with other analytics tooling load mechanism and also to reuse the exisiting efficient loading technique.

Related Cloud PR - dimagi/commcare-cloud#6279 for adding the GTM ID.

Feature Flag

Safety Assurance

Safety story

This is a fairly straight forward setup code for loading gtm installtion script. Similar to what we do for other tooling which makes changes safe.
Tested on local.
Testing on staging pending.

Automated test coverage

QA Plan

None

Rollback instructions

  • This PR can be reverted after deploy with no further considerations

Labels & Review

  • Risk label is set correctly
  • The set of people pinged as reviewers is appropriate for the level of risk of the change

@dimagimon dimagimon added the Risk: Medium Change affects files that have been flagged as medium risk. label May 3, 2024
@ajeety4 ajeety4 force-pushed the ay/google-tag-manager branch from cc539ca to b4e058d Compare May 6, 2024 07:58
@ajeety4 ajeety4 marked this pull request as ready for review May 6, 2024 11:11
@ajeety4 ajeety4 requested review from orangejenny and biyeun as code owners May 6, 2024 11:11
@ajeety4 ajeety4 added Open for review: do not merge A work in progress product/invisible Change has no end-user visible impact labels May 6, 2024
{% if request.couch_user %}
{% initial_analytics_data 'gtm.userId' request.couch_user.userID %}
{% initial_analytics_data 'gtm.userIsDimagi' request.couch_user.is_dimagi %}
{% initial_analytics_data 'gtm.userIsCommCare' request.couch_user.is_commcare_user %}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I'd suggest saving that as gtm.userIsCommCareUser since that makes it more clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed in ac89b8a

{% initial_analytics_data 'gtm.userIsCommCare' request.couch_user.is_commcare_user %}
{% endif %}
{% if domain %}
{% initial_analytics_data 'gtm.domain' domain|escapejs %}
Copy link
Contributor

Choose a reason for hiding this comment

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

why we are using escapejs here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for pointing this out.
I reused this code from google.html. I believe it was added to avoid XSS attack . After looking more into it and don't think it is needed here as we are capturing domain from the url and it should not allow js mixed into it.
Removed it in ac89b8a

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, in that case I say we should retain it.

I assume its possible for someone to put anything in the domain part in the url & HQ will look for it. No actual domain page would load but I assume this part still gets added to the page, hence the escapejs.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just a spitball suggestion (feel free to ignore):
I agree with Manish's concern about someone manually changing the domain part of the url, so either

  1. keep the escapejs filter
  2. access the domain through a way we know it's right, e.g. request.couch_user.domain (maybe even request.domain?)

Option 2 seems least pretty though.

Copy link
Contributor

Choose a reason for hiding this comment

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

If the domain is invalid, the page should have 404ed long before this point.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If the domain is invalid, the page should have 404ed long before this point.

Yes, this was tested and it is not possible to add script from the url.

@Charl1996 Correct me if I am wrong, my understanding is that domain for request or request.couch_user is still obtained from url ? (A user may be associated with multiple domains.)

Copy link
Contributor

Choose a reason for hiding this comment

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

That's true, but as far as I know there's some sanity checks it goes through in middleware such that it will fail if the domain is wrong (Jenny's comment maybe hints at this). That said, I haven't looked at the code.

j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','{{ ANALYTICS_IDS.GTM_ID }}');
</script>
Copy link
Contributor

Choose a reason for hiding this comment

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

where is this happening now? or its not needed anymore?

same for iframe that was removed later?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The loading of gtm.js now happens here hence excluded from html. This was
done to be in consistent with other analytics tooling load mechanism and also to reuse the existing efficient loading technique with logs and validations.

The iframe one was optional and is only for scenario where JavaScript is disabled.

Copy link
Contributor

Choose a reason for hiding this comment

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

How do we confirm this does not break anything that was being tracked already?

Copy link
Contributor

@orangejenny orangejenny May 9, 2024

Choose a reason for hiding this comment

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

Check in with SaaS tech about this, please.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was able to confirm that Google Tag manager is not used at present by checking below things

  • No option in Cloud PR to add GTM_ID without which above snippet is not executed.
  • Checked through browser tools that gtm.js is not loaded for Prod or India.

I think it is wise to double check with SAAS though.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hi @ajeety4
I believe you were able to check with SaaS about it? Are there changes coming from your discussion?

Copy link
Contributor Author

@ajeety4 ajeety4 May 22, 2024

Choose a reason for hiding this comment

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

Hey @mkangia , I checked with SAAS about this. They do not use it and suggested to check with Marketing.
I talked with Marketing and they do use Google Tag Manager, I am just waiting for more details on it, should be able to get that in next couple of days.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Update: We reached out to marketing and they do have a Google Tag manager account with a container for CommcareHQ. As expected, the container for CommcareHQ is empty and not currently being used and aligns with the current findings.

(Just for FYI - they have a container for www.dimagi.com which is currently in use)

Copy link
Contributor

Choose a reason for hiding this comment

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

So it is safe to remove this section or not?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes it is. It is safe even it was being used as the change just loads the same file from the javascript instead of the html.
I think it is still wise to merge this only after the decision to use GTM is finalised.
(No use to have the code that is not actively used or planned to be used.)

@ajeety4
Copy link
Contributor Author

ajeety4 commented Jun 19, 2024

Update - GTD has decided to go ahead with Google Analytics as the primary tooling so we will be leveraging Google Tag Manager along with GA.
Requesting for a final review.

@ajeety4 ajeety4 requested a review from zandre-eng June 19, 2024 12:57
@ajeety4
Copy link
Contributor Author

ajeety4 commented Jun 28, 2024

Hey @mkangia , requesting for a final pass on this.

@@ -0,0 +1,68 @@
"use strict";
Copy link
Contributor

Choose a reason for hiding this comment

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

is this copied from gtm?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, this script does the job of loading the analytics script and passing the desired user info here.
This is same approach used for loading other analytics tooling script.


### Google Tag Manager (GTM)

This script is available in the [gtm.js](https://github.com/dimagi/commcare-hq/blob/master/corehq/apps/analytics/static/analytix/js/gtm.js).Related code is just for sending the user properties to GTM.
Copy link
Contributor

@mkangia mkangia Jun 28, 2024

Choose a reason for hiding this comment

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

nit: Its script is available in gtm.js?

Copy link
Contributor

Choose a reason for hiding this comment

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

nit: what is referred to from "related code"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed in a15d0df.
"related code" is just the script code.

### Google Tag Manager (GTM)

This script is available in the [gtm.js](https://github.com/dimagi/commcare-hq/blob/master/corehq/apps/analytics/static/analytix/js/gtm.js).Related code is just for sending the user properties to GTM.
Any tracking of events should be configured at the GTM console end in tandem with the desired analytics tooling. The purpose is to track specific features in HQ and also disable them when there is no need via the GTM console itself.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: The goal is to track ...?

goal instead of purpose?

@@ -108,3 +108,5 @@ Their script is included in [signup](https://github.com/dimagi/commcare-hq/blob/

This script is available in the [gtm.js](https://github.com/dimagi/commcare-hq/blob/master/corehq/apps/analytics/static/analytix/js/gtm.js).Related code is just for sending the user properties to GTM.
Any tracking of events should be configured at the GTM console end in tandem with the desired analytics tooling. The purpose is to track specific features in HQ and also disable them when there is no need via the GTM console itself.
Any `id` attribute added to html element for tracking through console should be prefixed with `gtm-` to indicate that this element is likely being tracked in GTM.
This should potentially avoid accidental removal of id attribute from these elements. (Similar approach may be followed for any tooling in case of tracking of elements through console.)
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Copy link
Contributor

@mkangia mkangia left a comment

Choose a reason for hiding this comment

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

Apologies, I didn't know you were blocked for review on this. Looks good to me.
I see you didn't take this through so I assume you tested this well for all pages or any kinds of pages this would modify.

@ajeety4
Copy link
Contributor Author

ajeety4 commented Jul 1, 2024

Apologies, I didn't know you were blocked for review on this. Looks good to me. I see you didn't take this through so I assume you tested this well for all pages or any kinds of pages this would modify.

Thanks Manish.
This was tested on the staging environment and has been deployed there for quite a time now.

@ajeety4 ajeety4 merged commit fcb26dd into master Jul 3, 2024
11 of 12 checks passed
@ajeety4 ajeety4 deleted the ay/google-tag-manager branch July 3, 2024 08:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Open for review: do not merge A work in progress product/invisible Change has no end-user visible impact Risk: Medium Change affects files that have been flagged as medium risk.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants