Skip to content
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

Add google analytics log to form submission #35602

Merged
merged 14 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ hqDefine("cloudcare/js/form_entry/web_form_session", [
'cloudcare/js/form_entry/form_ui',
'cloudcare/js/formplayer/utils/utils',
'cloudcare/js/formplayer/users/models',
'cloudcare/js/gtx',
], function (
$,
ko,
Expand All @@ -22,6 +23,7 @@ hqDefine("cloudcare/js/form_entry/web_form_session", [
formUI,
utils,
UsersModels,
gtx,
) {
function WebFormSession(params) {
var self = {};
Expand Down Expand Up @@ -543,6 +545,11 @@ hqDefine("cloudcare/js/form_entry/web_form_session", [
function (resp) {
form.isSubmitting(false);
if (resp.status === 'success') {
const gtxEventData = {
title: form.title(),
breadcrumbs: form.breadcrumbs() ? form.breadcrumbs().join(">") : "",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you grab breadcrumbs here instead of selections? I'm guessing cause you don't have access to selections here?

};
gtx.logFormSubmit(gtxEventData);
self.onsubmit(resp);
} else {
$.each(resp.errors, function (ix, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ hqDefine("cloudcare/js/formplayer/menus/controller", [
'cloudcare/js/formplayer/menus/views/query',
'cloudcare/js/formplayer/menus/views',
'cloudcare/js/formplayer/menus/api', // app:select:menus and entity:get:details
'cloudcare/js/gtx',
], function (
$,
_,
Expand All @@ -33,7 +34,10 @@ hqDefine("cloudcare/js/formplayer/menus/controller", [
menusUtils,
queryView,
views,
api,
gtx,
) {

var selectMenu = function (options) {

options.preview = UsersModels.getCurrentUser().displayOptions.singleAppMode;
Expand All @@ -52,6 +56,8 @@ hqDefine("cloudcare/js/formplayer/menus/controller", [
return;
}

gtx.logNavigateMenu(gtx.extractSelections(menuResponse));

//set title of tab to application name
if (menuResponse.breadcrumbs) {
document.title = menuResponse.breadcrumbs[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ hqDefine("cloudcare/js/formplayer/menus/utils", [
'cloudcare/js/formplayer/users/models',
'cloudcare/js/formplayer/utils/utils',
'cloudcare/js/formplayer/menus/views',
'analytix/js/gtx',
'cloudcare/js/gtx',
], function (
_,
Backbone,
Expand Down Expand Up @@ -224,7 +224,7 @@ hqDefine("cloudcare/js/formplayer/menus/utils", [
moduleName: menuResponse.title,
});
kissmetrics.track.event("Viewed Case List", kissmetricsEventData);
gtx.sendEvent("web_apps_viewed_case_list", gtxEventData);
gtx.logCaseList(gtx.extractSelections(menuResponse), gtxEventData);

if (/search_command\.m\d+/.test(menuResponse.queryKey) && menuResponse.currentPage === 0) {
kissmetrics.track.event('Started Case Search', {
Expand Down
65 changes: 65 additions & 0 deletions corehq/apps/cloudcare/static/cloudcare/js/gtx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
hqDefine('cloudcare/js/gtx', [
'underscore',
'analytix/js/gtx',
], function (
_,
gtx,
) {
let lastCaseListSelections = "";
let lastCaseListTimeMs = Date.now();
let lastSelections = "";
let lastSelectionsChangeTimeMs = Date.now();

const extractSelections = function (menuResponse) {
return menuResponse.selections ? menuResponse.selections.join(">") : "";
};

const logNavigateMenu = function (selections) {
if (selections !== lastSelections) {
const navigatedAwayFromCaseList =
lastCaseListSelections !== "" && !selections.startsWith(lastCaseListSelections);
const gtxEventData = {
selections: selections,
previousSelections: lastSelections,
navigatedAwayFromCaseList: navigatedAwayFromCaseList,
caseListSelection: lastCaseListSelections,
timeSinceLastSelectionChangeMs: Date.now() - lastSelectionsChangeTimeMs,
timeSinceLastCaseListOpenMs: Date.now() - lastCaseListTimeMs,
};
lastSelections = selections;
lastSelectionsChangeTimeMs = Date.now();
if (navigatedAwayFromCaseList) {
lastCaseListSelections = "";
}
gtx.sendEvent("web_apps_selection_change", gtxEventData);
}
};

const logCaseList = function (selections, gtxEventData) {
_.extend(gtxEventData, {
selections: selections,
});
if (selections !== lastCaseListSelections) {
lastCaseListSelections = selections;
lastCaseListTimeMs = Date.now();
}
gtx.sendEvent("web_apps_viewed_case_list", gtxEventData);
};

const logFormSubmit = function (gtxEventData) {
_.extend(gtxEventData, {
navigatedAwayFromCaseList: true,
timeSinceLastCaseListOpenMs: Date.now() - lastCaseListTimeMs,
caseListSelection: lastCaseListSelections,
});
lastCaseListSelections = "";
gtx.sendEvent("web_apps_submit_form", gtxEventData);
};

return {
extractSelections: extractSelections,
logCaseList: logCaseList,
logFormSubmit: logFormSubmit,
logNavigateMenu: logNavigateMenu,
};
});