From fc19d7f492c08f17ee1a6bb96e942a0a41b9bf9a Mon Sep 17 00:00:00 2001 From: Alex Parsons Date: Wed, 29 May 2024 15:40:17 +0000 Subject: [PATCH] Start tracking donate button clicks - Old approach used functions expected in mysociety.org --- www/docs/js/main.js | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/www/docs/js/main.js b/www/docs/js/main.js index 35622ae423..6dd194a9f0 100644 --- a/www/docs/js/main.js +++ b/www/docs/js/main.js @@ -6,18 +6,23 @@ var trackEvent = function(eventName, params) { var dfd = $.Deferred(); var callback = function(){ - dfd.resolve(); + dfd.resolve(); }; - // Tell Gtag to resolve our promise when it's done. - var params = $.extend(params, { + if (typeof gtag !== 'undefined') { + // Tell Gtag to resolve our promise when it's done. + var params = $.extend(params, { event_callback: callback - }); + }); - gtag('event', eventName, params); + gtag('event', eventName, params); - // Wait a maximum of 2 seconds for Gtag to resolve promise. - setTimeout(callback, 2000); + // Wait a maximum of 2 seconds for Gtag to resolve promise. + setTimeout(callback, 2000); + } else { + // If gtag is not defined, e.g. in dev mode, resolve the promise immediately. + callback(); + } return dfd.promise(); }; @@ -478,15 +483,11 @@ $(function() { grecaptcha.execute(); }; - if (!window.analytics) { - return submitPaymentForm(); - } - - window.analytics.trackEvent( + trackEvent( "donate_form_submit", {"frequency": howoften, "value": amount } - ).done(submitPaymentForm); - }); - + ).always(submitPaymentForm); + }); + }); function onDonateError(message) {