-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into fix/use-map-clustering
- Loading branch information
Showing
6 changed files
with
156 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -253,4 +253,5 @@ angular | |
event.title = title; | ||
window.dispatchEvent(event); | ||
}); | ||
}]); | ||
}]) | ||
.run(require('./gtm-userprops.js')); |
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,74 @@ | ||
module.exports = [ | ||
'$q', | ||
'ConfigEndpoint', | ||
'UserEndpoint', | ||
function ($q, ConfigEndpoint, UserEndpoint) { | ||
// Utility for collecting user properties from application state and pushing | ||
// them to the datalayer | ||
function getUserPropertiesPromise() { | ||
var userProps = $q.defer(); | ||
|
||
var site = $q.defer(); | ||
var multisite = $q.defer(); | ||
var user = $q.defer(); | ||
|
||
ConfigEndpoint.get({id: 'site'}).$promise.then(function (x) { | ||
site.resolve(x); | ||
}); | ||
ConfigEndpoint.get({id: 'multisite'}).$promise.then(function (x) { | ||
multisite.resolve(x); | ||
}); | ||
UserEndpoint.get({id: 'me'}).$promise.then(function (x) { | ||
if (x.id && x.role) { | ||
// According to data dictionary: | ||
// https://docs.google.com/document/d/1ulZerckIGun-mv4ASu2nEFGFTpqcYwBXd3HR95eKj68 | ||
x.role_level = x.role === 'admin' ? 'admin' : 'member'; | ||
} | ||
user.resolve(x); | ||
}).catch(function () { | ||
user.resolve(null); | ||
}) | ||
|
||
$q.all([site.promise, multisite.promise, user.promise]).then(function (results) { | ||
var site = results[0] || {}; | ||
var multisite = results[1] || {}; | ||
var user = results[2] || {}; | ||
|
||
// This is a composition of the user id and deployment id, because each user must be unique | ||
// in the analytics data warehouse | ||
var scopedUserId = undefined; | ||
if (user.id) { | ||
scopedUserId = String(user.id) + ',' + String(multisite.site_id || null); | ||
} | ||
|
||
userProps.resolve({ | ||
deployment_url: multisite.site_fqdn || window.location.host , | ||
deployment_id: multisite.site_id || null, | ||
deployment_name: site.name || undefined, | ||
user_id: scopedUserId, | ||
user_role: user.role_level || undefined, | ||
browser_language: navigator.language | ||
? navigator.languages[0] | ||
: (navigator.language || navigator.userLanguage) | ||
}); | ||
}); | ||
|
||
return userProps; | ||
} | ||
|
||
function triggerRefresh() { | ||
getUserPropertiesPromise().promise.then(function (userProps) { | ||
let event = new CustomEvent('datalayer:userprops', { detail: userProps }); | ||
window.dispatchEvent(event); | ||
}); | ||
} | ||
|
||
// Initialize user properties along with this module | ||
triggerRefresh(); | ||
|
||
// Add event listener to refresh user properties on demand | ||
window.addEventListener('ush:analytics:refreshUserProperties', function () { | ||
triggerRefresh(); | ||
}); | ||
|
||
}]; |
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,65 @@ | ||
class Datalayer { | ||
constructor() { | ||
this.pushQ = []; | ||
this.userProperties = {}; | ||
} | ||
|
||
initialize() { | ||
// Listen to user property change events | ||
window.addEventListener("datalayer:userprops", ({detail}) => { | ||
this.userProperties = detail; | ||
if (this.pushQ.length > 0) { | ||
for (var obj of this.pushQ) { | ||
obj.user_properties = this.userProperties; | ||
this.push(obj); | ||
} | ||
this.pushQ = []; | ||
} else { | ||
this.push({user_properties: this.userProperties}); | ||
} | ||
}); | ||
|
||
// Listen to custom events to be pushed | ||
window.addEventListener("datalayer:custom-event", ({detail}) => { | ||
this.push(detail); | ||
}); | ||
|
||
// Watching for routing events | ||
window.addEventListener("single-spa:routing-event", | ||
({ detail: { newUrl } }) => { | ||
const path = new URL(newUrl).pathname; | ||
const pageType = this.pathToPageType(path); | ||
|
||
// Corner case: routing event before user properties set | ||
if (Object.keys(this.userProperties).length > 0) { | ||
this.push({'page_type': pageType, user_properties: this.userProperties}); | ||
} else { | ||
// If user properties have not been initialised yet, we should queue | ||
this.pushQ.push({'page_type': pageType}); | ||
} | ||
}); | ||
} | ||
|
||
push(obj) { | ||
window.dataLayer = window.dataLayer || []; | ||
window.dataLayer.push({ ecommerce: null }); | ||
window.dataLayer.push(obj); | ||
console.log("pushed"); | ||
console.log(obj); | ||
} | ||
|
||
pathToPageType(path) { | ||
// i.e. '/settings/general' -> ['settings', 'general'] | ||
const tokens = path.split('/').filter(Boolean); | ||
|
||
if (tokens[0] == 'settings') { | ||
return 'deployment-settings'; | ||
} else if (tokens[0] == 'activity') { | ||
return 'deployment-activity'; | ||
} else { | ||
return 'deployment-other'; | ||
} | ||
} | ||
} | ||
|
||
export const datalayer = new Datalayer(); |
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