From 6c9ece3352ed4625e520265a8e416817e6b824bc Mon Sep 17 00:00:00 2001 From: Jeff Mohan Date: Tue, 12 Mar 2024 10:31:15 -0700 Subject: [PATCH 1/7] Add gtagEvent helper function --- packages/client/src/helpers/gtag.js | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/packages/client/src/helpers/gtag.js b/packages/client/src/helpers/gtag.js index 0225ba6eb..418d3c5a3 100644 --- a/packages/client/src/helpers/gtag.js +++ b/packages/client/src/helpers/gtag.js @@ -2,6 +2,12 @@ // Note that the 'set' command does not appear to do anything (despite Google documentation to the contrary) so all // runtime reconfiguration is being performed via 'config'. +function gtag(...args) { + if (typeof window.gtag === 'function' && window.APP_CONFIG?.GOOGLE_TAG_ID) { + window.gtag(...args); + } +} + /** * If Google Analytics are enabled, calls the config command on the appropriate Google tag. * If called repeatedly, appears to update the config by performing a deep merge of the existing config with the new. @@ -13,12 +19,10 @@ */ export function gtagConfig(config) { // See index.html for the definition of the gtag function. - if (typeof window.gtag === 'function' && window.APP_CONFIG?.GOOGLE_TAG_ID) { - window.gtag('config', window.APP_CONFIG.GOOGLE_TAG_ID, { - ...(window.APP_CONFIG?.GOOGLE_ANALYTICS_DEBUG ? { debug_mode: true } : {}), - ...config, - }); - } + gtag('config', window.APP_CONFIG.GOOGLE_TAG_ID, { + ...(window.APP_CONFIG?.GOOGLE_ANALYTICS_DEBUG ? { debug_mode: true } : {}), + ...config, + }); } export function setUserForGoogleAnalytics(user) { @@ -30,3 +34,13 @@ export function setUserForGoogleAnalytics(user) { }, }); } + +/** + * Emits a Google Analytics GA4 custom event. + * + * @param {string} eventName Canonical name of the event to emit + * @param {object} eventParams Metadata to associate with this event + */ +export function gtagEvent(eventName, eventParams) { + gtag('event', eventName, eventParams); +} From 1a82690ec2ba453389089bb6aa2d787c313b65b0 Mon Sep 17 00:00:00 2001 From: Jeff Mohan Date: Tue, 12 Mar 2024 10:44:48 -0700 Subject: [PATCH 2/7] Add logging when gtag commands are emitted --- packages/client/src/helpers/gtag.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/client/src/helpers/gtag.js b/packages/client/src/helpers/gtag.js index 418d3c5a3..7542d453e 100644 --- a/packages/client/src/helpers/gtag.js +++ b/packages/client/src/helpers/gtag.js @@ -5,6 +5,11 @@ function gtag(...args) { if (typeof window.gtag === 'function' && window.APP_CONFIG?.GOOGLE_TAG_ID) { window.gtag(...args); + if (window.APP_CONFIG?.GOOGLE_ANALYTICS_DEBUG) { + console.log(`[Google Analytics] Emitted ${args[0]} with:`, ...args.slice(1)); + } + } else if (window.APP_CONFIG?.GOOGLE_ANALYTICS_DEBUG) { + console.log(`[Google Analytics][DISABLED] Would have emitted ${args[0]} with:`, ...args.slice(1)); } } From 737879444323041527d7d2e3c21e0ad2ef923ae3 Mon Sep 17 00:00:00 2001 From: Jeff Mohan Date: Tue, 12 Mar 2024 10:57:19 -0700 Subject: [PATCH 3/7] Instrument three grants.gov, copy, and print buttons --- packages/client/src/helpers/gtag.js | 3 ++- packages/client/src/views/GrantDetails.vue | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/client/src/helpers/gtag.js b/packages/client/src/helpers/gtag.js index 7542d453e..08e22d15e 100644 --- a/packages/client/src/helpers/gtag.js +++ b/packages/client/src/helpers/gtag.js @@ -41,7 +41,8 @@ export function setUserForGoogleAnalytics(user) { } /** - * Emits a Google Analytics GA4 custom event. + * Emits a Google Analytics GA4 custom event. Google also defines some reserved param names to be aware of, + * such as `event_callback` to execute code once the event has been logged: https://developers.google.com/tag-platform/gtagjs/reference/parameters * * @param {string} eventName Canonical name of the event to emit * @param {object} eventParams Metadata to associate with this event diff --git a/packages/client/src/views/GrantDetails.vue b/packages/client/src/views/GrantDetails.vue index a44ee4d09..300d8c3e8 100644 --- a/packages/client/src/views/GrantDetails.vue +++ b/packages/client/src/views/GrantDetails.vue @@ -49,6 +49,7 @@ target="_blank" rel="noopener noreferrer" data-dd-action-name="view on grants.gov" + @click="onOpenGrantsGov" >