From bb0f8266496569e3f4bd53c0dd408a5ed5c99857 Mon Sep 17 00:00:00 2001 From: amywieliczka Date: Wed, 26 Apr 2023 22:34:09 -0700 Subject: [PATCH 1/6] Update to add matomo tracking code to Calisphere --- app/scripts/ItemView.js | 52 +++++++++++++++++++ app/scripts/calisphere.js | 43 +++++++++++++++ calisphere/templates/400.html | 15 ++++++ calisphere/templates/403.html | 16 ++++++ calisphere/templates/404.html | 4 ++ calisphere/templates/500.html | 15 ++++++ .../templates/calisphere/ga_site_code.html | 17 ++++++ public_interface/context_processors.py | 1 + public_interface/settings.py | 1 + 9 files changed, 164 insertions(+) diff --git a/app/scripts/ItemView.js b/app/scripts/ItemView.js index 9500242f8..6925ecbf5 100644 --- a/app/scripts/ItemView.js +++ b/app/scripts/ItemView.js @@ -60,6 +60,10 @@ var ItemView = Backbone.View.extend({ action = 'open request modal'; break; } + if (typeof _paq !== 'undefined') { + var dimensions = get_cali_ga_dimensions(); + _paq.push(['trackEvent', category, action, label, undefined, dimensions]); + } if (typeof ga !== 'undefined') { var dimensions = get_cali_ga_dimensions(); ga('caliga.send', 'event', @@ -94,6 +98,14 @@ var ItemView = Backbone.View.extend({ }, carouselAfterChange: function(e, slick) { + if (typeof _paq !== 'undefined') { + var dimensions = get_cali_ga_dimensions(); + _paq.push(['trackEvent', + 'related content', + 'paginate items', + $('.carousel__search-results').data('set'), + undefined, dimensions]); + } if (typeof ga !== 'undefined') { var dimensions = get_cali_ga_dimensions(); ga('caliga.send', 'event', @@ -167,6 +179,14 @@ var ItemView = Backbone.View.extend({ }, exhibitCarouselBeforeChange: function() { + if (typeof _paq !== 'undefined') { + var dimensions = get_cali_ga_dimensions(); + _paq.push(['trackEvent', + 'related content', + 'paginate exhibitions', + $('.carousel__search-results').data('set'), + undefined, dimensions]); + } if (typeof ga !== 'undefined') { var dimensions = get_cali_ga_dimensions(); ga('caliga.send', 'event', @@ -178,6 +198,14 @@ var ItemView = Backbone.View.extend({ }, goToExhibition: function() { + if (typeof _paq !== 'undefined') { + var dimensions = get_cali_ga_dimensions(); + _paq.push(['trackEvent', + 'related content', + 'select exhibition', + $('.carousel__search-results').data('set'), + undefined, dimensions]); + } if (typeof ga !== 'undefined') { var dimensions = get_cali_ga_dimensions(); ga('caliga.send', 'event', @@ -223,6 +251,14 @@ var ItemView = Backbone.View.extend({ if ( e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey ) { return; } if ($(e.currentTarget).data('item_number') !== undefined) { + if (typeof _paq !== 'undefined') { + var dimensions = get_cali_ga_dimensions(); + _paq.push(['trackEvent', + 'related content', + 'select item', + $('.carousel__search-results').data('set'), + undefined, dimensions]); + } if (typeof ga !== 'undefined') { var dimensions = get_cali_ga_dimensions(); ga('caliga.send', 'event', @@ -263,6 +299,14 @@ var ItemView = Backbone.View.extend({ // don't need carousel specific item data for the related collections delete data_params.itemNumber; if (e !== undefined) { + if (typeof _paq !== 'undefined') { + var dimensions = get_cali_ga_dimensions(); + _paq.push(['trackEvent', + 'related content', + 'paginate collections', + $('.carousel__search-results').data('set'), + undefined, dimensions]); + } if (typeof ga !== 'undefined') { var dimensions = get_cali_ga_dimensions(); ga('caliga.send', 'event', @@ -339,6 +383,14 @@ var ItemView = Backbone.View.extend({ traditional: true }); } else { + if (typeof _paq !== 'undefined') { + var dimensions = get_cali_ga_dimensions(); + _paq.push(['trackEvent', + 'related content', + 'select collection', + $('.carousel__search-results').data('set'), + undefined, dimensions]); + } if (typeof ga !== 'undefined') { var dimensions = get_cali_ga_dimensions(); ga('caliga.send', 'event', diff --git a/app/scripts/calisphere.js b/app/scripts/calisphere.js index 894d276b9..b29c0c8d1 100644 --- a/app/scripts/calisphere.js +++ b/app/scripts/calisphere.js @@ -117,6 +117,42 @@ $(document).ready(function() { return false; }); } + if (typeof _paq !== 'undefined' ) { + var outboundSelector = 'a[href^="http://"], a[href^="https://"]'; + outboundSelector += ', button[onclick^="location.href\=\'http\:\/\/"]'; + outboundSelector += ', button[onclick^="location.href\=\'https\:\/\/"]'; + $('body').on('click', outboundSelector, function() { + var url = ''; + if($(this).attr('href')) { + url = $(this).attr('href'); + } else if($(this).attr('onclick')) { + var c = $(this).attr('onclick'); + url = c.slice(15, c.length-2); + } + + var dimensions = get_cali_ga_dimensions(); + hitCallback = timeoutGACallback(function(){ + // click captured and tracked, send the user along + document.location = url; + }) + // https://developer.matomo.org/guides/tracking-javascript-guide#tracking-a-custom-dimension-for-one-specific-action-only + _paq.push(['trackEvent', 'outbound', 'click', url, + undefined, dimensions, hitCallback]); + return false; + }); + + $('.button__contact-owner').on('click', function() { + var url = $(this).attr('href'); + + var dimensions = get_cali_ga_dimensions(); + hitCallback = timeoutGACallback(function() { + document.location = url; + }); + _paq.push(['trackEvent', 'buttons', 'contact', url, + undefined, dimensions, hitCallback]); + return false; + }); + } // *********************************** // Add banner notice for users without session storage @@ -249,6 +285,13 @@ var on_ready_pjax_end_handler = function() { // send google analytics on pjax pages /* globals ga: false */ /* jshint latedef: false */ + if (typeof _paq !== 'undefined') { + var dimensions = get_cali_ga_dimensions(); + _paq.push(['setCustomUrl', window.location.href]); + _paq.push(['setDocumentTitle', document.title]); + _paq.push(['trackPageView', document.title, dimensions]); + _paq.push(['enableLinkTracking']); + } if (typeof ga !== 'undefined') { var dimensions = get_cali_ga_dimensions(); ga('caliga.set', 'location', window.location.href); diff --git a/calisphere/templates/400.html b/calisphere/templates/400.html index 5c4d9a7e8..631f0ec93 100644 --- a/calisphere/templates/400.html +++ b/calisphere/templates/400.html @@ -39,6 +39,21 @@ ga('require', 'linkid'); ga('set', 'anonymizeIp', true); + + +
diff --git a/calisphere/templates/403.html b/calisphere/templates/403.html index 40b592e54..7b3e67eee 100644 --- a/calisphere/templates/403.html +++ b/calisphere/templates/403.html @@ -38,6 +38,22 @@ ga('require', 'linkid'); ga('set', 'anonymizeIp', true); + + + +
diff --git a/calisphere/templates/404.html b/calisphere/templates/404.html index 5dec3ec91..45dd4e11e 100644 --- a/calisphere/templates/404.html +++ b/calisphere/templates/404.html @@ -212,5 +212,9 @@

404: Not Found

ga('caliga.set', 'location', window.location.href); ga('caliga.send', 'event', 'error', '404', 'source: django'); } + if (typeof _paq !== 'undefined') { + _paq.push(['setCustomUrl', window.location.href]); + _paq.push(['trackEvent', 'error', '404', 'source: django']); + } diff --git a/calisphere/templates/500.html b/calisphere/templates/500.html index dc0b5cac7..af0d2415b 100644 --- a/calisphere/templates/500.html +++ b/calisphere/templates/500.html @@ -39,6 +39,21 @@ ga('require', 'linkid'); ga('set', 'anonymizeIp', true); + + +
diff --git a/calisphere/templates/calisphere/ga_site_code.html b/calisphere/templates/calisphere/ga_site_code.html index f72cc6598..88c981b9a 100644 --- a/calisphere/templates/calisphere/ga_site_code.html +++ b/calisphere/templates/calisphere/ga_site_code.html @@ -23,3 +23,20 @@ gtag('config', '{{ ga4SiteCode }}'); {% endif %} + +{% if matomoSiteCode %} + + + + +{% endif %} \ No newline at end of file diff --git a/public_interface/context_processors.py b/public_interface/context_processors.py index baae45f50..ec113d890 100644 --- a/public_interface/context_processors.py +++ b/public_interface/context_processors.py @@ -30,6 +30,7 @@ def settings(request): 'ucldcNuxeoThumbs': settings.UCLDC_NUXEO_THUMBS, 'gaSiteCode': settings.GA_SITE_CODE, 'ga4SiteCode': settings.GA4_SITE_CODE, + 'matomoSiteCode': settings.MATOMO_SITE_CODE, 'contactFlag': settings.CONTRUBUTOR_CONTACT_FLAG, 'permalink': permalink, 'multiple_indexes': multiple_indexes, diff --git a/public_interface/settings.py b/public_interface/settings.py index d1e220342..3a58cbf74 100644 --- a/public_interface/settings.py +++ b/public_interface/settings.py @@ -85,6 +85,7 @@ def getenv(variable, default): GA_SITE_CODE = getenv('UCLDC_GA_SITE_CODE', False) GA4_SITE_CODE = getenv('UCLDC_GA4_SITE_CODE', False) +MATOMO_SITE_CODE = getenv('UCLDC_MATOMO_SITE_CODE', False) UCLDC_WALKME = getenv('UCLDC_WALKME', False) # SECURITY WARNING: don't run with debug turned on in production! From 67c94dd533c9f79d51afd71730be4af80f768b4e Mon Sep 17 00:00:00 2001 From: amywieliczka Date: Thu, 27 Apr 2023 09:04:45 -0700 Subject: [PATCH 2/6] Add page view tracking on homepage --- calisphere/templates/calisphere/home.html | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/calisphere/templates/calisphere/home.html b/calisphere/templates/calisphere/home.html index 729990742..2f1dca45f 100644 --- a/calisphere/templates/calisphere/home.html +++ b/calisphere/templates/calisphere/home.html @@ -22,6 +22,12 @@ document.getElementsByTagName('html')[0].classList.remove('no-jquery'); {% include "calisphere/ga_site_code.html" %} + From 100869857cd8f5e003c94038be922b04a5f57d6b Mon Sep 17 00:00:00 2001 From: amywieliczka Date: Thu, 27 Apr 2023 09:14:54 -0700 Subject: [PATCH 3/6] linted js --- app/scripts/ItemView.js | 24 ++++++++++-------------- app/scripts/calisphere.js | 32 ++++++++++++++------------------ 2 files changed, 24 insertions(+), 32 deletions(-) diff --git a/app/scripts/ItemView.js b/app/scripts/ItemView.js index 6925ecbf5..59ef6d2fe 100644 --- a/app/scripts/ItemView.js +++ b/app/scripts/ItemView.js @@ -61,8 +61,10 @@ var ItemView = Backbone.View.extend({ break; } if (typeof _paq !== 'undefined') { - var dimensions = get_cali_ga_dimensions(); - _paq.push(['trackEvent', category, action, label, undefined, dimensions]); + _paq.push([ + 'trackEvent', category, action, + label, undefined, get_cali_ga_dimensions() + ]); } if (typeof ga !== 'undefined') { var dimensions = get_cali_ga_dimensions(); @@ -99,12 +101,11 @@ var ItemView = Backbone.View.extend({ carouselAfterChange: function(e, slick) { if (typeof _paq !== 'undefined') { - var dimensions = get_cali_ga_dimensions(); _paq.push(['trackEvent', 'related content', 'paginate items', $('.carousel__search-results').data('set'), - undefined, dimensions]); + undefined, get_cali_ga_dimensions()]); } if (typeof ga !== 'undefined') { var dimensions = get_cali_ga_dimensions(); @@ -180,12 +181,11 @@ var ItemView = Backbone.View.extend({ exhibitCarouselBeforeChange: function() { if (typeof _paq !== 'undefined') { - var dimensions = get_cali_ga_dimensions(); _paq.push(['trackEvent', 'related content', 'paginate exhibitions', $('.carousel__search-results').data('set'), - undefined, dimensions]); + undefined, get_cali_ga_dimensions()]); } if (typeof ga !== 'undefined') { var dimensions = get_cali_ga_dimensions(); @@ -199,12 +199,11 @@ var ItemView = Backbone.View.extend({ goToExhibition: function() { if (typeof _paq !== 'undefined') { - var dimensions = get_cali_ga_dimensions(); _paq.push(['trackEvent', 'related content', 'select exhibition', $('.carousel__search-results').data('set'), - undefined, dimensions]); + undefined, get_cali_ga_dimensions()]); } if (typeof ga !== 'undefined') { var dimensions = get_cali_ga_dimensions(); @@ -252,12 +251,11 @@ var ItemView = Backbone.View.extend({ if ($(e.currentTarget).data('item_number') !== undefined) { if (typeof _paq !== 'undefined') { - var dimensions = get_cali_ga_dimensions(); _paq.push(['trackEvent', 'related content', 'select item', $('.carousel__search-results').data('set'), - undefined, dimensions]); + undefined, get_cali_ga_dimensions()]); } if (typeof ga !== 'undefined') { var dimensions = get_cali_ga_dimensions(); @@ -300,12 +298,11 @@ var ItemView = Backbone.View.extend({ delete data_params.itemNumber; if (e !== undefined) { if (typeof _paq !== 'undefined') { - var dimensions = get_cali_ga_dimensions(); _paq.push(['trackEvent', 'related content', 'paginate collections', $('.carousel__search-results').data('set'), - undefined, dimensions]); + undefined, get_cali_ga_dimensions()]); } if (typeof ga !== 'undefined') { var dimensions = get_cali_ga_dimensions(); @@ -384,12 +381,11 @@ var ItemView = Backbone.View.extend({ }); } else { if (typeof _paq !== 'undefined') { - var dimensions = get_cali_ga_dimensions(); _paq.push(['trackEvent', 'related content', 'select collection', $('.carousel__search-results').data('set'), - undefined, dimensions]); + undefined, get_cali_ga_dimensions()]); } if (typeof ga !== 'undefined') { var dimensions = get_cali_ga_dimensions(); diff --git a/app/scripts/calisphere.js b/app/scripts/calisphere.js index b29c0c8d1..6b8f46f03 100644 --- a/app/scripts/calisphere.js +++ b/app/scripts/calisphere.js @@ -82,10 +82,11 @@ $(document).ready(function() { // based on https://support.google.com/analytics/answer/1136920?hl=en // We capture the click handler on outbound links and the contact owner button + var outboundSelector = 'a[href^="http://"], a[href^="https://"]'; + outboundSelector += ', button[onclick^="location.href\=\'http\:\/\/"]'; + outboundSelector += ', button[onclick^="location.href\=\'https\:\/\/"]'; + if (typeof ga !== 'undefined') { - var outboundSelector = 'a[href^="http://"], a[href^="https://"]'; - outboundSelector += ', button[onclick^="location.href\=\'http\:\/\/"]'; - outboundSelector += ', button[onclick^="location.href\=\'https\:\/\/"]'; $('body').on('click', outboundSelector, function() { var url = ''; if($(this).attr('href')) { @@ -118,9 +119,6 @@ $(document).ready(function() { }); } if (typeof _paq !== 'undefined' ) { - var outboundSelector = 'a[href^="http://"], a[href^="https://"]'; - outboundSelector += ', button[onclick^="location.href\=\'http\:\/\/"]'; - outboundSelector += ', button[onclick^="location.href\=\'https\:\/\/"]'; $('body').on('click', outboundSelector, function() { var url = ''; if($(this).attr('href')) { @@ -130,26 +128,24 @@ $(document).ready(function() { url = c.slice(15, c.length-2); } - var dimensions = get_cali_ga_dimensions(); - hitCallback = timeoutGACallback(function(){ - // click captured and tracked, send the user along - document.location = url; - }) // https://developer.matomo.org/guides/tracking-javascript-guide#tracking-a-custom-dimension-for-one-specific-action-only _paq.push(['trackEvent', 'outbound', 'click', url, - undefined, dimensions, hitCallback]); + undefined, get_cali_ga_dimensions(), + timeoutGACallback(function(){ + // click captured and tracked, send the user along + document.location = url; + })]); return false; }); $('.button__contact-owner').on('click', function() { var url = $(this).attr('href'); - var dimensions = get_cali_ga_dimensions(); - hitCallback = timeoutGACallback(function() { - document.location = url; - }); _paq.push(['trackEvent', 'buttons', 'contact', url, - undefined, dimensions, hitCallback]); + undefined, get_cali_ga_dimensions(), + timeoutGACallback(function() { + document.location = url; + })]); return false; }); } @@ -283,7 +279,7 @@ var cluster_search = function(col_id, facet_field) { var on_ready_pjax_end_handler = function() { // send google analytics on pjax pages - /* globals ga: false */ + /* globals ga: false, _paq: false */ /* jshint latedef: false */ if (typeof _paq !== 'undefined') { var dimensions = get_cali_ga_dimensions(); From 3ce2d240157b3e1be3638825285a8fce8b0f5a2f Mon Sep 17 00:00:00 2001 From: amywieliczka Date: Thu, 27 Apr 2023 09:19:17 -0700 Subject: [PATCH 4/6] matomo linting --- app/scripts/ItemView.js | 2 +- app/scripts/calisphere.js | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/scripts/ItemView.js b/app/scripts/ItemView.js index 59ef6d2fe..93f373654 100644 --- a/app/scripts/ItemView.js +++ b/app/scripts/ItemView.js @@ -1,4 +1,4 @@ -/*global Backbone, DISQUS, ga, get_cali_ga_dimensions, get_inst_ga_dimensions */ +/*global Backbone, DISQUS, ga, _paq, get_cali_ga_dimensions, get_inst_ga_dimensions */ /*exported ItemView */ 'use strict'; diff --git a/app/scripts/calisphere.js b/app/scripts/calisphere.js index 6b8f46f03..a6ef0ce9d 100644 --- a/app/scripts/calisphere.js +++ b/app/scripts/calisphere.js @@ -282,10 +282,9 @@ var on_ready_pjax_end_handler = function() { /* globals ga: false, _paq: false */ /* jshint latedef: false */ if (typeof _paq !== 'undefined') { - var dimensions = get_cali_ga_dimensions(); _paq.push(['setCustomUrl', window.location.href]); _paq.push(['setDocumentTitle', document.title]); - _paq.push(['trackPageView', document.title, dimensions]); + _paq.push(['trackPageView', document.title, get_cali_ga_dimensions()]); _paq.push(['enableLinkTracking']); } if (typeof ga !== 'undefined') { From c394d05633a2cc8a09b633aa0e752379f9b1672d Mon Sep 17 00:00:00 2001 From: amywieliczka Date: Tue, 27 Jun 2023 15:58:01 -0700 Subject: [PATCH 5/6] Update matomo tracker to point to prod matomo --- calisphere/templates/400.html | 4 ++-- calisphere/templates/403.html | 4 ++-- calisphere/templates/500.html | 4 ++-- calisphere/templates/calisphere/ga_site_code.html | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/calisphere/templates/400.html b/calisphere/templates/400.html index 631f0ec93..006c34c85 100644 --- a/calisphere/templates/400.html +++ b/calisphere/templates/400.html @@ -46,9 +46,9 @@ _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { - var u="https://analytics.cdlib.org/matomo/"; + var u="//matomo.cdlib.org/"; _paq.push(['setTrackerUrl', u+'matomo.php']); - _paq.push(['setSiteId', '8']); + _paq.push(['setSiteId', '5']); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); })(); diff --git a/calisphere/templates/403.html b/calisphere/templates/403.html index 7b3e67eee..465557c1c 100644 --- a/calisphere/templates/403.html +++ b/calisphere/templates/403.html @@ -45,9 +45,9 @@ _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { - var u="https://analytics.cdlib.org/matomo/"; + var u="//matomo.cdlib.org/"; _paq.push(['setTrackerUrl', u+'matomo.php']); - _paq.push(['setSiteId', '8']); + _paq.push(['setSiteId', '5']); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); })(); diff --git a/calisphere/templates/500.html b/calisphere/templates/500.html index af0d2415b..fcebe538c 100644 --- a/calisphere/templates/500.html +++ b/calisphere/templates/500.html @@ -46,9 +46,9 @@ _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { - var u="https://analytics.cdlib.org/matomo/"; + var u="//matomo.cdlib.org/"; _paq.push(['setTrackerUrl', u+'matomo.php']); - _paq.push(['setSiteId', '8']); + _paq.push(['setSiteId', '5']); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); })(); diff --git a/calisphere/templates/calisphere/ga_site_code.html b/calisphere/templates/calisphere/ga_site_code.html index 88c981b9a..9c27c7345 100644 --- a/calisphere/templates/calisphere/ga_site_code.html +++ b/calisphere/templates/calisphere/ga_site_code.html @@ -30,9 +30,9 @@ var _paq = window._paq = window._paq || []; /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ (function() { - var u="https://analytics.cdlib.org/matomo/"; + var u="//matomo.cdlib.org/"; _paq.push(['setTrackerUrl', u+'matomo.php']); - _paq.push(['setSiteId', '{{ matomoSiteCode }}']); + _paq.push(['setSiteId', '5']); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); })(); From 32128df7ee4e75ed2b8d36f8ec00874ca2210424 Mon Sep 17 00:00:00 2001 From: amywieliczka Date: Tue, 27 Jun 2023 16:04:52 -0700 Subject: [PATCH 6/6] Use env var to specify matomo site number --- calisphere/templates/calisphere/ga_site_code.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/calisphere/templates/calisphere/ga_site_code.html b/calisphere/templates/calisphere/ga_site_code.html index 9c27c7345..9b01e6f7f 100644 --- a/calisphere/templates/calisphere/ga_site_code.html +++ b/calisphere/templates/calisphere/ga_site_code.html @@ -32,7 +32,7 @@ (function() { var u="//matomo.cdlib.org/"; _paq.push(['setTrackerUrl', u+'matomo.php']); - _paq.push(['setSiteId', '5']); + _paq.push(['setSiteId', '{{ matomoSiteCode }}']); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); })();