Skip to content

Commit

Permalink
Merge branch 'develop' into fix/use-map-clustering
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxpiper authored Jun 27, 2022
2 parents b596dc3 + 559f82e commit db2c4a9
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app/config.js.j2
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ window.ushahidi = {
gaKey: '{{ GA_KEY }}',
{% else %}
gaEnabled: false,
{% endif %}
{% if GTM_CONTAINER_ID %}
googleTagManager: '{{ GTM_CONTAINER_ID }}',
{% endif %}
intercomAppId: '{{ INTERCOM_APPID }}',
appStoreId: '{{ APP_STORE_ID }}',
Expand Down
3 changes: 2 additions & 1 deletion legacy/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,5 @@ angular
event.title = title;
window.dispatchEvent(event);
});
}]);
}])
.run(require('./gtm-userprops.js'));
10 changes: 10 additions & 0 deletions legacy/app/auth/authentication.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ function (
language: userData.language
});
loginStatus = true;

// Refresh the user properties due to the change
window.dispatchEvent(new CustomEvent('ush:analytics:refreshUserProperties'));
window.dispatchEvent(new CustomEvent('datalayer:custom-event', {
detail: {
event: 'user logged in',
event_type: 'user_interaction',
user_role: userData.role === 'admin' ? 'admin' : 'member'
}
}));
}

function continueLogout(silent) {
Expand Down
74 changes: 74 additions & 0 deletions legacy/app/gtm-userprops.js
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();
});

}];
65 changes: 65 additions & 0 deletions root/src/datalayer.js
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();
2 changes: 2 additions & 0 deletions root/src/ushahidi-root-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "single-spa-layout";
import microfrontendLayout from "./microfrontend-layout.html";
import { getPageMetadata, setBootstrapConfig } from "@ushahidi/utilities";
import { datalayer } from "./datalayer.js";

require("./loading.scss");

Expand All @@ -24,6 +25,7 @@ const layoutEngine = constructLayoutEngine({ routes, applications });

applications.forEach(registerApplication);
layoutEngine.activate();
datalayer.initialize();

const showError = function (show) {
const element = document.querySelector('#bootstrap-error');
Expand Down

0 comments on commit db2c4a9

Please sign in to comment.