From 3201300caa457fd3647190176585ae2b4ebc2118 Mon Sep 17 00:00:00 2001 From: j-mys Date: Tue, 4 Jun 2024 13:10:04 +0300 Subject: [PATCH] add JavaScript tracking option for Matomo analytics --- dist/js/matomo.min.js | 2 +- src/js/matomo.js | 70 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/dist/js/matomo.min.js b/dist/js/matomo.min.js index 9d2e783511..378ba8cbac 100644 --- a/dist/js/matomo.min.js +++ b/dist/js/matomo.min.js @@ -1 +1 @@ -!function(e,Drupal){function t(){if(void 0!==Drupal.eu_cookie_compliance&&Drupal.eu_cookie_compliance.hasAgreed("statistics")){(window._mtm=window._mtm||[]).push({"mtm.startTime":(new Date).getTime(),event:"mtm.Start"});const e=document,t=e.createElement("script"),i=e.getElementsByTagName("script")[0];t.type="text/javascript",t.async=!0,t.src="//webanalytics.digiaiiris.com/js/container_X3dUNyXY.js",i.parentNode.insertBefore(t,i)}}e(document).on("eu_cookie_compliance.changeStatus",t()),e(document).ready(t)}(jQuery,Drupal); \ No newline at end of file +!function(){function e(){let e=null;window.innerWidth?e=window.innerWidth:document.documentElement&&document.documentElement.clientWidth?e=document.documentElement.clientWidth:document.body&&document.body.clientWidth&&(e=document.body.clientWidth),"number"==typeof e&&(e=10*Math.round(e/10));let t=null;return window.innerHeight?t=window.innerHeight:document.documentElement&&document.documentElement.clientHeight?t=document.documentElement.clientHeight:document.body&&document.body.clientHeight&&(t=document.body.clientHeight),"number"==typeof t&&(t=10*Math.round(t/10)),e+"x"+t}!function(t,Drupal){function n(){const t="?9mt5bfb2bGk="==window.location.search;if(t){console.info("Using JavaScript Tracking");const t=()=>window.innerWidth,i=()=>window.innerHeight,o=()=>document.querySelector("html")?.attributes?.lang?.value||"unkown",s=()=>document.querySelector('meta[property="article:published_time"]')?.content||"",a=()=>document.querySelector('meta[property="og:updated_time"]')?.content||"";var n=window._paq=window._paq||[];n.push(["setExcludedQueryParams",["maintheme","channeltype","errandservicetype","targetgroup","year","doc","ls","vdoc","dkey","pno","dir","buttonName","familyIncome","fulltime","daysoff","familySize","submitButton","cn","pd","sivu","tyoPaikkaAla","HELP_MODE","tyoSanaHakuKentta","SHOW_TOOLS","haeTyopaikkojaBTN","nayta-kesa","redir","chatMode","id","Id","feedbackId","fid","up","p","c","tyoSuhtTyyppi","kielisyys","cityarea","selection","contentViewMode","v","INFO_MODE","EDIT_MODE","PAGE_MODES","showyears","categories","categories2","MOD","newfeatures","format","contentID","useDefaultText","useDefaultDesc","li_fat_id","lmod","CACHEID","__FB_PRIVATE_TRACKING__","as_qdr","as_occt","as_q","contentIDR","id-2939","redir3","urile","sa","ved","resetButton","hcb","cep","service_node","setlanguage","continueFlag","nayta-kaikki","trk","existed","logout","attachedData","next","ref_ttesl_hdh_ep1","sort","customerid","CVID","amp","1dmy","osoite","mailto","Siirtyy%20jaoston%20päätösasiakirjoihin.","Siirtyy%20sivutossa%20jaoston%20päätösasiakirjojen%20kohtaan.","v:file","v:state","btnG","current","readclass","safe","/'javascript:?%27%20class=%27ch2-open-settings-btn%27%20onClick=%27cookiehub.openSettings()%27","row","start","end","mode","zarsrc","gws_rd","showfromdate","^.*\\@hel.fi$","' class","%27%20class","class","_sm_au_","action","Siirtyy jaoston päätösasiakirjoihin.","Siirtyy sivutossa jaoston päätösasiakirjojen kohtaan.","ISCI","usein kysyttyä palveluverkkosuunnittelusta","Pelastussuunnitelma","Palotarkastus","recepient","Action","amp;current","XIe","dNe","mc_cid","mc_eid","added","client-request-id","estsrequest","ceid","emid","crmid","identify","elq","elqaid","elqat","classId","assignmentId","gidzl","_hsmi","_hsenc","tre","trete","pe_data","o4e","n4e","check_logged_in","code","u4e","itok","WCM_PORTLET","SessionExpired","KYe","WCM_GLOBAL_CONTEXT","frosmo","C_e","name","state","ttclid","acaToken","wvstest","max-depth","__proto__[crVsaSZqMnW4EqBoI22emA]","__proto__.crVsaSZqMnW4EqBoI22emA","constructor.prototype[crVsaSZqMnW4EqBoI22emA]","constructor.prototype.crVsaSZqMnW4EqBoI22emA","constructor[prototype][crVsaSZqMnW4EqBoI22emA]","gsid","fbclid","time","complianz_scan_token","complianz_id"]]),n.push(["setCustomDimension",2,t()]),n.push(["setCustomDimension",3,i()]),n.push(["setCustomDimension",4,o()]),n.push(["setCustomDimension",5,s()]),n.push(["setCustomDimension",6,a()]),n.push(["setCustomDimension",7,e()]),n.push(["trackPageView"]),n.push(["enableLinkTracking"]),function(){var e="//webanalytics.digiaiiris.com/js/";n.push(["setTrackerUrl",e+"tracker.php"]),n.push(["setSiteId","141"]);var t=document,i=t.createElement("script"),o=t.getElementsByTagName("script")[0];i.async=!0,i.src=e+"piwik.min.js",o.parentNode.insertBefore(i,o)}()}if((t||void 0!==Drupal.eu_cookie_compliance)&&!t&&Drupal.eu_cookie_compliance.hasAgreed("statistics")){(window._mtm=window._mtm||[]).push({"mtm.startTime":(new Date).getTime(),event:"mtm.Start"});const e=document,t=e.createElement("script"),n=e.getElementsByTagName("script")[0];t.type="text/javascript",t.async=!0,t.src="//webanalytics.digiaiiris.com/js/container_X3dUNyXY.js",n.parentNode.insertBefore(t,n)}}t(document).on("eu_cookie_compliance.changeStatus",n()),t(document).ready(n)}(jQuery,Drupal)}(); \ No newline at end of file diff --git a/src/js/matomo.js b/src/js/matomo.js index 2892a4b108..9536923a19 100644 --- a/src/js/matomo.js +++ b/src/js/matomo.js @@ -1,12 +1,51 @@ // eslint-disable-next-line func-names (function ($, Drupal) { function loadMatomoAnalytics() { - if (typeof Drupal.eu_cookie_compliance === 'undefined') { + /** + * If the queryparameter is found, the script will be loaded + * regardless of cookie consents etc. + */ + const useJSAPI = window.location.search === '?9mt5bfb2bGk='; + if (useJSAPI) { + // eslint-disable-next-line no-console + console.info('Using JavaScript Tracking'); + const getViewportWidth = () => window.innerWidth; + const getViewportHeight = () => window.innerHeight; + const getLanguage = () => document.querySelector('html')?.attributes?.lang?.value || 'unkown'; + const getPublishedTime = () => document.querySelector('meta[property="article:published_time"]')?.content || ''; + const getUpdatedTime = () => document.querySelector('meta[property="og:updated_time"]')?.content || ''; + // eslint-disable-next-line no-multi-assign + const _paq = window._paq = window._paq || []; + // eslint-disable-next-line no-useless-escape + /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ + // eslint-disable-next-line no-useless-escape + _paq.push(['setExcludedQueryParams', ['maintheme','channeltype','errandservicetype','targetgroup','year','doc','ls','vdoc','dkey','pno','dir','buttonName','familyIncome','fulltime','daysoff','familySize','submitButton','cn','pd','sivu','tyoPaikkaAla','HELP_MODE','tyoSanaHakuKentta','SHOW_TOOLS','haeTyopaikkojaBTN','nayta-kesa','redir','chatMode','id','Id','feedbackId','fid','up','p','c','tyoSuhtTyyppi','kielisyys','cityarea','selection','contentViewMode','v','INFO_MODE','EDIT_MODE','PAGE_MODES','showyears','categories','categories2','MOD','newfeatures','format','contentID','useDefaultText','useDefaultDesc','li_fat_id','lmod','CACHEID','__FB_PRIVATE_TRACKING__','as_qdr','as_occt','as_q','contentIDR','id-2939','redir3','urile','sa','ved','resetButton','hcb','cep','service_node','setlanguage','continueFlag','nayta-kaikki','trk','existed','logout','attachedData','next','ref_ttesl_hdh_ep1','sort','customerid','CVID','amp','1dmy','osoite','mailto','Siirtyy%20jaoston%20p\u00e4\u00e4t\u00f6sasiakirjoihin.','Siirtyy%20sivutossa%20jaoston%20p\u00e4\u00e4t\u00f6sasiakirjojen%20kohtaan.','v:file','v:state','btnG','current','readclass','safe','\/\'javascript:?%27%20class=%27ch2-open-settings-btn%27%20onClick=%27cookiehub.openSettings()%27','row','start','end','mode','zarsrc','gws_rd','showfromdate','^.*\\@hel.fi$','\' class','%27%20class','class','_sm_au_','action','Siirtyy jaoston p\u00e4\u00e4t\u00f6sasiakirjoihin.','Siirtyy sivutossa jaoston p\u00e4\u00e4t\u00f6sasiakirjojen kohtaan.','ISCI','usein kysytty\u00e4 palveluverkkosuunnittelusta','Pelastussuunnitelma','Palotarkastus','recepient','Action','amp;current','XIe','dNe','mc_cid','mc_eid','added','client-request-id','estsrequest','ceid','emid','crmid','identify','elq','elqaid','elqat','classId','assignmentId','gidzl','_hsmi','_hsenc','tre','trete','pe_data','o4e','n4e','check_logged_in','code','u4e','itok','WCM_PORTLET','SessionExpired','KYe','WCM_GLOBAL_CONTEXT','frosmo','C_e','name','state','ttclid','acaToken','wvstest','max-depth','__proto__[crVsaSZqMnW4EqBoI22emA]','__proto__.crVsaSZqMnW4EqBoI22emA','constructor.prototype[crVsaSZqMnW4EqBoI22emA]','constructor.prototype.crVsaSZqMnW4EqBoI22emA','constructor[prototype][crVsaSZqMnW4EqBoI22emA]','gsid','fbclid','time','complianz_scan_token','complianz_id']]); + _paq.push(['setCustomDimension', 2, getViewportWidth()]); + _paq.push(['setCustomDimension', 3, getViewportHeight()]); + _paq.push(['setCustomDimension', 4, getLanguage()]); + _paq.push(['setCustomDimension', 5, getPublishedTime()]); + _paq.push(['setCustomDimension', 6, getUpdatedTime()]); + // eslint-disable-next-line no-use-before-define + _paq.push(['setCustomDimension', 7, getBrowserSize()]); + _paq.push(['trackPageView']); + _paq.push(['enableLinkTracking']); + // eslint-disable-next-line func-names + (function() { + const u='//webanalytics.digiaiiris.com/js/'; + _paq.push(['setTrackerUrl', `${u}tracker.php`]); + _paq.push(['setSiteId', '141']); + const d=document; const g=d.createElement('script'); const s=d.getElementsByTagName('script')[0]; + // Consider integrity hash check + g.async=true; g.src=`${u}piwik.min.js`; s.parentNode.insertBefore(g,s); + })(); + } + + if (!useJSAPI && typeof Drupal.eu_cookie_compliance === 'undefined') { return; } // Load Matomo only if statistics cookies are allowed. - if (Drupal.eu_cookie_compliance.hasAgreed('statistics')) { + if (!useJSAPI && Drupal.eu_cookie_compliance.hasAgreed('statistics')) { // Matomo Tag Manager // eslint-disable-next-line no-multi-assign const _mtm = (window._mtm = window._mtm || []); @@ -30,3 +69,30 @@ // Load on page load. $(document).ready(loadMatomoAnalytics); })(jQuery, Drupal); + +// Clean/refactor this file when the testing phase is done +function getBrowserSize() { + let width = null; + if (window.innerWidth) { + width = window.innerWidth; + } else if (document.documentElement && document.documentElement.clientWidth) { + width = document.documentElement.clientWidth; + } else if (document.body && document.body.clientWidth) { + width = document.body.clientWidth; + } + if (typeof width === 'number') { + width = Math.round(width / 10) * 10; + } + let height = null; + if (window.innerHeight) { + height = window.innerHeight; + } else if (document.documentElement && document.documentElement.clientHeight) { + height = document.documentElement.clientHeight; + } else if (document.body && document.body.clientHeight) { + height = document.body.clientHeight; + } + if (typeof height === 'number') { + height = Math.round(height / 10) * 10; + } + return `${width}x${height}`; +}