-
-
Notifications
You must be signed in to change notification settings - Fork 218
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
Google Tag Manager setup #34554
Conversation
cc539ca
to
b4e058d
Compare
{% 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 %} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 %} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
- keep the escapejs filter
- access the domain through a way we know it's right, e.g.
request.couch_user.domain
(maybe evenrequest.domain
?)
Option 2 seems least pretty though.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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> |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.)
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. |
Hey @mkangia , requesting for a final pass on this. |
@@ -0,0 +1,68 @@ | |||
"use strict"; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
corehq/apps/analytics/README.md
Outdated
|
||
### 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. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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"?
There was a problem hiding this comment.
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.
corehq/apps/analytics/README.md
Outdated
### 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. |
There was a problem hiding this comment.
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.) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this 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.
Thanks Manish. |
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
Labels & Review