-
-
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
Changes from 7 commits
ac89b8a
404b9a0
d1c138f
c18f556
4e5aad8
ee0283c
b4e058d
5152a86
6cf6903
a15d0df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,3 +103,8 @@ Generally available in areas of interest to the product and growth teams: signup | |
### Facebook Pixel | ||
|
||
Their script is included in [signup](https://github.com/dimagi/commcare-hq/blob/master/corehq/apps/registration/templates/registration/register_new_user.html), but we don't do any event tracking or other interaction with it. Very little related code, just the script inclusion. | ||
|
||
### 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 commentThe reason will be displayed to describe this comment to others. Learn more. nit: The goal is to track ...? goal instead of purpose? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
"use strict"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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. |
||
/** | ||
* Handles communication with the google tag manager API. | ||
*/ | ||
hqDefine('analytix/js/gtm', [ | ||
'jquery', | ||
'underscore', | ||
'analytix/js/initial', | ||
'analytix/js/logging', | ||
'analytix/js/utils', | ||
], function ( | ||
$, | ||
_, | ||
initialAnalytics, | ||
logging, | ||
utils | ||
) { | ||
var _get = initialAnalytics.getFn('gtm'), | ||
_logger = logging.getLoggerForApi('Google Tag Manager'), | ||
_ready = $.Deferred(); | ||
|
||
window.dataLayer = window.dataLayer || []; | ||
|
||
/** | ||
* Helper function to send event to Google Tag Manager. | ||
* @param {string} eventName | ||
* @param {object} eventData | ||
* @param {function|undefined} callbackFn - optional | ||
*/ | ||
var gtmSendEvent = function (eventName, eventData, callbackFn) { | ||
_ready.done(function () { | ||
var data = { | ||
event: eventName, | ||
}; | ||
if (eventData) { | ||
_.extend(data, eventData); | ||
} | ||
window.dataLayer.push(data); | ||
_logger.verbose.log(eventName, 'window.dataLayer.push'); | ||
}).fail(function () { | ||
if (_.isFunction(callbackFn)) { | ||
callbackFn(); | ||
} | ||
}); | ||
}; | ||
|
||
$(function () { | ||
var apiId = _get('apiId'), | ||
scriptUrl = '//www.googletagmanager.com/gtm.js?id=' + apiId; | ||
|
||
_ready = utils.initApi(_ready, apiId, scriptUrl, _logger, function () { | ||
var userProperties = { | ||
userId: _get('userId', 'none'), | ||
isDimagi: _get('userIsDimagi', 'no', 'yes'), | ||
isCommCare: _get('userIsCommCare', 'no', 'yes'), | ||
domain: _get('domain', 'none'), | ||
hqEnvironment: _get('hqInstance', 'none'), | ||
}; | ||
// userProperties are sent first to be available for use as early as possible | ||
gtmSendEvent('userProperties', userProperties); | ||
gtmSendEvent('gtm.js', {'gtm.start': new Date().getTime()}); | ||
}); | ||
}); | ||
|
||
return { | ||
sendEvent: gtmSendEvent, | ||
}; | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{% load hq_shared_tags %} | ||
{% initial_analytics_data 'gtm.apiId' ANALYTICS_IDS.GTM_ID %} | ||
{% 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 commentThe reason will be displayed to describe this comment to others. Learn more. nit: I'd suggest saving that as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in ac89b8a |
||
{% endif %} | ||
{% if domain %} | ||
{% initial_analytics_data 'gtm.domain' domain|escapejs %} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for pointing this out. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Just a spitball suggestion (feel free to ignore):
Option 2 seems least pretty though. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more.
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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
{% endif %} | ||
{% if ANALYTICS_CONFIG.HQ_INSTANCE %} | ||
{% initial_analytics_data 'gtm.hqInstance' ANALYTICS_CONFIG.HQ_INSTANCE %} | ||
{% endif %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,19 +6,6 @@ | |
<!--[if IE 8]><html lang="{{ LANGUAGE_CODE }}" class="lt-ie9"><![endif]--> | ||
<!--[if gt IE 8]><!--><html lang="{{ LANGUAGE_CODE }}"><!--<![endif]--> | ||
<head> | ||
{% if ANALYTICS_IDS.GTM_ID %} | ||
{# This is fine as an inline script; it's independent of all HQ code and all third-party libraries #} | ||
<!-- Google Tag Manager --> | ||
<script> | ||
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': | ||
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], | ||
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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. The loading of 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 commentThe 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 commentThe 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 commentThe 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
I think it is wise to double check with SAAS though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @ajeety4 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 (Just for FYI - they have a container for There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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. |
||
<!-- End Google Tag Manager --> | ||
{% endif %} | ||
|
||
{% captureas title_block %}{% block title %}{% endblock title %}{% endcaptureas %} | ||
{% captureas title_context_block %}{% block title_context %}{% endblock title_context %}{% endcaptureas %} | ||
<title> | ||
|
@@ -187,13 +174,6 @@ | |
{% endblock %} | ||
</head> | ||
<body> | ||
{% if ANALYTICS_IDS.GTM_ID %} | ||
<!-- Google Tag Manager (noscript) --> | ||
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id={{ ANALYTICS_IDS.GTM_ID }}" | ||
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript> | ||
<!-- End Google Tag Manager (noscript) --> | ||
{% endif %} | ||
|
||
{# for setting up page-wide backgrounds #} | ||
{% block background_content %}{% endblock %} | ||
|
||
|
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.