-
-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'ay/google-tag-manager' into autostaging
- Loading branch information
Showing
11 changed files
with
94 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
"use strict"; | ||
/** | ||
* 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('userIsCommCareUser', '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, | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
corehq/apps/analytics/templates/analytics/initial/gtm.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.userIsCommCareUser' request.couch_user.is_commcare_user %} | ||
{% endif %} | ||
{% if domain %} | ||
{% initial_analytics_data 'gtm.domain' domain %} | ||
{% endif %} | ||
{% if ANALYTICS_CONFIG.HQ_INSTANCE %} | ||
{% initial_analytics_data 'gtm.hqInstance' ANALYTICS_CONFIG.HQ_INSTANCE %} | ||
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters