From ef68a84fc9c47b6c5d4d74c3023af2b9dd03bc13 Mon Sep 17 00:00:00 2001 From: Mike Crantea Date: Sun, 28 Jul 2024 11:36:01 +0100 Subject: [PATCH 1/6] Add support for GA4 setup through GTM and the window.dataLayer --- js/src/send-to-analytics.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/js/src/send-to-analytics.js b/js/src/send-to-analytics.js index 1bae3ba..ac466ce 100644 --- a/js/src/send-to-analytics.js +++ b/js/src/send-to-analytics.js @@ -14,10 +14,7 @@ * limitations under the License. */ -function getDeliveryFunction( type ) { - // eslint-disable-next-line no-console - return window[ type ] || console.log; -} +/* global gtag */ export function sendToAnalytics( { name, value, delta, id, attribution, rating } ) { const analyticsData = window.webVitalsAnalyticsData?.[ 0 ] ?? null; @@ -76,6 +73,16 @@ export function sendToAnalytics( { name, value, delta, id, attribution, rating } } if ( analyticsData && analyticsData.ga4_id ) { - getDeliveryFunction( 'gtag' )( 'event', name, eventParams ); + if ( typeof gtag === 'function' ) { + gtag( 'event', name, eventParams ); + } else if ( window.dataLayer && typeof window.dataLayer.push === 'function' ) { + window.dataLayer.push( { + event: name, + ...eventParams, + } ); + } else { + // eslint-disable-next-line no-console + console.log( 'Event:', name, eventParams ); + } } } From 8645a15129db7739424df1d2a422915cea35042d Mon Sep 17 00:00:00 2001 From: Mike Crantea Date: Sun, 28 Jul 2024 16:36:21 +0100 Subject: [PATCH 2/6] Update changelog, version --- README.md | 4 ++++ .../web-vitals-analytics.26c9e8650a743f1768edb15ad2170f0f.js | 1 + js/dist/module/web-vitals-analytics.asset.php | 2 +- .../web-vitals-analytics.e53aec45ee02272dfd68cf28c8f0a8cc.js | 1 - site-performance-tracker.php | 2 +- 5 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 js/dist/module/web-vitals-analytics.26c9e8650a743f1768edb15ad2170f0f.js delete mode 100644 js/dist/module/web-vitals-analytics.e53aec45ee02272dfd68cf28c8f0a8cc.js diff --git a/README.md b/README.md index 5a39200..a44da93 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,10 @@ All contributions are welcome! Please create [an issue](https://github.com/xwp/s ### Changelog +#### 1.3.5 - Jul 28th, 2023 + +- Add tracking support for Google Tag Manager setup GA4 with window.dataLayer. + #### 1.3.4 - Jul 15th, 2023 - Fix bug in INP reporting when some attributions were empty. diff --git a/js/dist/module/web-vitals-analytics.26c9e8650a743f1768edb15ad2170f0f.js b/js/dist/module/web-vitals-analytics.26c9e8650a743f1768edb15ad2170f0f.js new file mode 100644 index 0000000..0b4755d --- /dev/null +++ b/js/dist/module/web-vitals-analytics.26c9e8650a743f1768edb15ad2170f0f.js @@ -0,0 +1 @@ +(()=>{"use strict";function t({name:t,value:e,delta:n,id:r,attribution:i,rating:a}){const o=window.webVitalsAnalyticsData?.[0]??null,s={value:n,metric_id:r,metric_value:e,metric_delta:Math.round("CLS"===t?1e3*n:n),metric_rating:a};switch(t){case"CLS":s.debug_target=i?.largestShiftTarget||"(not set)";break;case"INP":const{processingDuration:t=0,presentationDelay:e=0,interactionTarget:n="(not set)",interactionType:r="(not set)",inputDelay:a=0}=i||{},o=i?.longAnimationFrameEntries?.at(-1),c=o?.scripts?.sort(((t,e)=>e.duration-t.duration))[0],u={inputDelay:a,processingDuration:t,presentationDelay:e};s.processingDuration=Math.round(t),s.presentationDelay=Math.round(e),s.debug_target=n,s.interactionType=r;const f=Object.keys(u).reduce(((t,e)=>u[t]>u[e]?t:e));if(u[f]>50&&(s.maxDelay=f),c){const{invokerType:t="(not set)",invoker:e="(not set)",sourceURL:n="(not set)",sourceCharPosition:r=0,sourceFunctionName:i="(not set)"}=c,{startTime:a=0,duration:u=0,styleAndLayoutStart:f=0}=o||{},d=a+u-f;s.invokerType=t,s.invoker=e,s.sourceURL=n,s.sourceCharPosition=r,s.sourceFunctionName=i,s.styleLayoutDuration=Math.round(d)}break;case"LCP":s.debug_target=i?.element||"(not set)";break;default:return"(not set)"}o&&o.ga4_id&&("function"==typeof gtag?gtag("event",t,s):window.dataLayer&&"function"==typeof window.dataLayer.push?window.dataLayer.push({event:t,...s}):console.log("Event:",t,s))}var e,n,r=function(){var t=self.performance&&performance.getEntriesByType&&performance.getEntriesByType("navigation")[0];if(t&&t.responseStart>0&&t.responseStart(e||100)-1)return n||i;if(n=n?i+">"+n:i,r.id)break;t=r.parentNode}}catch(t){}return n},s=-1,c=function(){return s},u=function(t){addEventListener("pageshow",(function(e){e.persisted&&(s=e.timeStamp,t(e))}),!0)},f=function(){var t=r();return t&&t.activationStart||0},d=function(t,e){var n=r(),i="navigate";return c()>=0?i="back-forward-cache":n&&(document.prerendering||f()>0?i="prerender":document.wasDiscarded?i="restore":n.type&&(i=n.type.replace(/_/g,"-"))),{name:t,value:void 0===e?-1:e,rating:"good",delta:0,entries:[],id:"v4-".concat(Date.now(),"-").concat(Math.floor(8999999999999*Math.random())+1e12),navigationType:i}},l=function(t,e,n){try{if(PerformanceObserver.supportedEntryTypes.includes(t)){var r=new PerformanceObserver((function(t){Promise.resolve().then((function(){e(t.getEntries())}))}));return r.observe(Object.assign({type:t,buffered:!0},n||{})),r}}catch(t){}},m=function(t,e,n,r){var i,a;return function(o){e.value>=0&&(o||r)&&((a=e.value-(i||0))||void 0===i)&&(i=e.value,e.delta=a,e.rating=function(t,e){return t>e[1]?"poor":t>e[0]?"needs-improvement":"good"}(e.value,n),t(e))}},p=function(t){requestAnimationFrame((function(){return requestAnimationFrame((function(){return t()}))}))},v=function(t){document.addEventListener("visibilitychange",(function(){"hidden"===document.visibilityState&&t()}))},g=function(t){var e=!1;return function(){e||(t(),e=!0)}},h=-1,y=function(){return"hidden"!==document.visibilityState||document.prerendering?1/0:0},T=function(t){"hidden"===document.visibilityState&&h>-1&&(h="visibilitychange"===t.type?t.timeStamp:0,S())},E=function(){addEventListener("visibilitychange",T,!0),addEventListener("prerenderingchange",T,!0)},S=function(){removeEventListener("visibilitychange",T,!0),removeEventListener("prerenderingchange",T,!0)},b=function(){return h<0&&(h=y(),E(),u((function(){setTimeout((function(){h=y(),E()}),0)}))),{get firstHiddenTime(){return h}}},w=function(t){document.prerendering?addEventListener("prerenderingchange",(function(){return t()}),!0):t()},L=[1800,3e3],C=function(t,e){e=e||{},w((function(){var n,r=b(),i=d("FCP"),a=l("paint",(function(t){t.forEach((function(t){"first-contentful-paint"===t.name&&(a.disconnect(),t.startTimee.latency){if(n)t.duration>n.latency?(n.entries=[t],n.latency=t.duration):t.duration===n.latency&&t.startTime===n.entries[0].startTime&&n.entries.push(t);else{var r={id:t.interactionId,latency:t.duration,entries:[t]};F.set(r.id,r),P.push(r)}P.sort((function(t,e){return e.latency-t.latency})),P.length>10&&P.splice(10).forEach((function(t){return F.delete(t.id)}))}}},j=function(t){var e=self.requestIdleCallback||self.setTimeout,n=-1;return t=g(t),"hidden"===document.visibilityState?t():(n=e(t),v(t)),n},q=[200,500],N=[],H=new Map,U=[],V=new WeakMap,W=new Map,z=-1,G=function(t){t.forEach((function(t){return N.push(t)}))},J=function(){W.size>10&&W.forEach((function(t,e){F.has(e)||W.delete(e)})),U=U.slice(-50);var t=new Set(U.concat(P.map((function(t){return V.get(t.entries[0])}))));H.forEach((function(e,n){t.has(n)||H.delete(n)}));var e=new Set;H.forEach((function(t){K(t.startTime,t.processingEnd).forEach((function(t){e.add(t)}))})),N=Array.from(e),z=-1};R.push((function(t){t.interactionId&&t.target&&!W.has(t.interactionId)&&W.set(t.interactionId,t.target)}),(function(t){for(var e,n=t.startTime+t.duration,r=U.length-1;r>=0;r--)if(e=U[r],Math.abs(n-e)<=8){var i=H.get(e);i.startTime=Math.min(t.startTime,i.startTime),i.processingStart=Math.min(t.processingStart,i.processingStart),i.processingEnd=Math.max(t.processingEnd,i.processingEnd),i.entries.push(t),n=e;break}n!==e&&(U.push(n),H.set(n,{startTime:t.startTime,processingStart:t.processingStart,processingEnd:t.processingEnd,entries:[t]})),(t.interactionId||"first-input"===t.entryType)&&V.set(t,n)}),(function(){z<0&&(z=j(J))}));var K=function(t,e){for(var n,r=[],i=0;n=N[i];i++)if(!(n.startTime+n.duratione)break;r.push(n)}return r},Q=function(t,e){n||(n=l("long-animation-frame",G)),function(t,e){e=e||{},w((function(){var n;x();var r,i=d("INP"),a=function(t){t.forEach(_);var e,n=(e=Math.min(P.length-1,Math.floor(O()/50)),P[e]);n&&n.latency!==i.value&&(i.value=n.latency,i.entries=n.entries,r())},o=l("event",a,{durationThreshold:null!==(n=e.durationThreshold)&&void 0!==n?n:40});r=m(t,i,q,e.reportAllChanges),o&&("PerformanceEventTiming"in self&&"interactionId"in PerformanceEventTiming.prototype&&o.observe({type:"first-input",buffered:!0}),v((function(){a(o.takeRecords()),r(!0)})),u((function(){B=0,P.length=0,F.clear(),i=d("INP"),r=m(t,i,q,e.reportAllChanges)})))}))}((function(e){j((function(){var n=function(t){var e=t.entries[0],n=V.get(e),r=H.get(n),a=e.processingStart,s=r.processingEnd,c=r.entries.sort((function(t,e){return t.processingStart-e.processingStart})),u=K(e.startTime,s),f=t.entries.find((function(t){return t.target})),d=f&&f.target||W.get(e.interactionId),l=[e.startTime+e.duration,s].concat(u.map((function(t){return t.startTime+t.duration}))),m=Math.max.apply(Math,l),p={interactionTarget:o(d),interactionTargetElement:d,interactionType:e.name.startsWith("key")?"keyboard":"pointer",interactionTime:e.startTime,nextPaintTime:m,processedEventEntries:c,longAnimationFrameEntries:u,inputDelay:a-e.startTime,processingDuration:s-a,presentationDelay:Math.max(m-s,0),loadState:i(e.startTime)};return Object.assign(t,{attribution:p})}(e);t(n)}))}),e)},X=[2500,4e3],Y={},Z=[800,1800],$=function t(e){document.prerendering?w((function(){return t(e)})):"complete"!==document.readyState?addEventListener("load",(function(){return t(e)}),!0):setTimeout(e,0)},tt=function(t,e){e=e||{};var n=d("TTFB"),i=m(t,n,Z,e.reportAllChanges);$((function(){var a=r();a&&(n.value=Math.max(a.responseStart-f(),0),n.entries=[a],i(!0),u((function(){n=d("TTFB",0),(i=m(t,n,Z,e.reportAllChanges))(!0)})))}))};new Date;function et(){!function(t,e){!function(t,e){e=e||{},C(g((function(){var n,r=d("CLS",0),i=0,a=[],o=function(t){t.forEach((function(t){if(!t.hadRecentInput){var e=a[0],n=a[a.length-1];i&&t.startTime-n.startTime<1e3&&t.startTime-e.startTime<5e3?(i+=t.value,a.push(t)):(i=t.value,a=[t])}})),i>r.value&&(r.value=i,r.entries=a,n())},s=l("layout-shift",o);s&&(n=m(t,r,D,e.reportAllChanges),v((function(){o(s.takeRecords()),n(!0)})),u((function(){i=0,r=d("CLS",0),n=m(t,r,D,e.reportAllChanges),p((function(){return n()}))})),setTimeout(n,0))})))}((function(e){var n=function(t){var e,n={};if(t.entries.length){var r=t.entries.reduce((function(t,e){return t&&t.value>e.value?t:e}));if(r&&r.sources&&r.sources.length){var a=(e=r.sources).find((function(t){return t.node&&1===t.node.nodeType}))||e[0];a&&(n={largestShiftTarget:o(a.node),largestShiftTime:r.startTime,largestShiftValue:r.value,largestShiftSource:a,largestShiftEntry:r,loadState:i(r.startTime)})}}return Object.assign(t,{attribution:n})}(e);t(n)}),e)}(t),function(t,e){C((function(e){var n=function(t){var e={timeToFirstByte:0,firstByteToFCP:t.value,loadState:i(c())};if(t.entries.length){var n=r(),a=t.entries[t.entries.length-1];if(n){var o=n.activationStart||0,s=Math.max(0,n.responseStart-o);e={timeToFirstByte:s,firstByteToFCP:t.value-s,loadState:i(t.entries[0].startTime),navigationEntry:n,fcpEntry:a}}}return Object.assign(t,{attribution:e})}(e);t(n)}),e)}(t),function(t,e){!function(t,e){e=e||{},w((function(){var n,r=b(),i=d("LCP"),a=function(t){e.reportAllChanges||(t=t.slice(-1)),t.forEach((function(t){t.startTime array(), 'version' => 'e53aec45ee02272dfd68cf28c8f0a8cc'); + array(), 'version' => '26c9e8650a743f1768edb15ad2170f0f'); diff --git a/js/dist/module/web-vitals-analytics.e53aec45ee02272dfd68cf28c8f0a8cc.js b/js/dist/module/web-vitals-analytics.e53aec45ee02272dfd68cf28c8f0a8cc.js deleted file mode 100644 index 6dc37a2..0000000 --- a/js/dist/module/web-vitals-analytics.e53aec45ee02272dfd68cf28c8f0a8cc.js +++ /dev/null @@ -1 +0,0 @@ -(()=>{"use strict";function t({name:t,value:e,delta:n,id:r,attribution:i,rating:a}){const o=window.webVitalsAnalyticsData?.[0]??null,s={value:n,metric_id:r,metric_value:e,metric_delta:Math.round("CLS"===t?1e3*n:n),metric_rating:a};switch(t){case"CLS":s.debug_target=i?.largestShiftTarget||"(not set)";break;case"INP":const{processingDuration:t=0,presentationDelay:e=0,interactionTarget:n="(not set)",interactionType:r="(not set)",inputDelay:a=0}=i||{},o=i?.longAnimationFrameEntries?.at(-1),c=o?.scripts?.sort(((t,e)=>e.duration-t.duration))[0],u={inputDelay:a,processingDuration:t,presentationDelay:e};s.processingDuration=Math.round(t),s.presentationDelay=Math.round(e),s.debug_target=n,s.interactionType=r;const f=Object.keys(u).reduce(((t,e)=>u[t]>u[e]?t:e));if(u[f]>50&&(s.maxDelay=f),c){const{invokerType:t="(not set)",invoker:e="(not set)",sourceURL:n="(not set)",sourceCharPosition:r=0,sourceFunctionName:i="(not set)"}=c,{startTime:a=0,duration:u=0,styleAndLayoutStart:f=0}=o||{},d=a+u-f;s.invokerType=t,s.invoker=e,s.sourceURL=n,s.sourceCharPosition=r,s.sourceFunctionName=i,s.styleLayoutDuration=Math.round(d)}break;case"LCP":s.debug_target=i?.element||"(not set)";break;default:return"(not set)"}o&&o.ga4_id&&(window["gtag"]||console.log)("event",t,s)}var e,n,r=function(){var t=self.performance&&performance.getEntriesByType&&performance.getEntriesByType("navigation")[0];if(t&&t.responseStart>0&&t.responseStart(e||100)-1)return n||i;if(n=n?i+">"+n:i,r.id)break;t=r.parentNode}}catch(t){}return n},s=-1,c=function(){return s},u=function(t){addEventListener("pageshow",(function(e){e.persisted&&(s=e.timeStamp,t(e))}),!0)},f=function(){var t=r();return t&&t.activationStart||0},d=function(t,e){var n=r(),i="navigate";return c()>=0?i="back-forward-cache":n&&(document.prerendering||f()>0?i="prerender":document.wasDiscarded?i="restore":n.type&&(i=n.type.replace(/_/g,"-"))),{name:t,value:void 0===e?-1:e,rating:"good",delta:0,entries:[],id:"v4-".concat(Date.now(),"-").concat(Math.floor(8999999999999*Math.random())+1e12),navigationType:i}},l=function(t,e,n){try{if(PerformanceObserver.supportedEntryTypes.includes(t)){var r=new PerformanceObserver((function(t){Promise.resolve().then((function(){e(t.getEntries())}))}));return r.observe(Object.assign({type:t,buffered:!0},n||{})),r}}catch(t){}},m=function(t,e,n,r){var i,a;return function(o){e.value>=0&&(o||r)&&((a=e.value-(i||0))||void 0===i)&&(i=e.value,e.delta=a,e.rating=function(t,e){return t>e[1]?"poor":t>e[0]?"needs-improvement":"good"}(e.value,n),t(e))}},p=function(t){requestAnimationFrame((function(){return requestAnimationFrame((function(){return t()}))}))},v=function(t){document.addEventListener("visibilitychange",(function(){"hidden"===document.visibilityState&&t()}))},g=function(t){var e=!1;return function(){e||(t(),e=!0)}},h=-1,y=function(){return"hidden"!==document.visibilityState||document.prerendering?1/0:0},T=function(t){"hidden"===document.visibilityState&&h>-1&&(h="visibilitychange"===t.type?t.timeStamp:0,S())},E=function(){addEventListener("visibilitychange",T,!0),addEventListener("prerenderingchange",T,!0)},S=function(){removeEventListener("visibilitychange",T,!0),removeEventListener("prerenderingchange",T,!0)},b=function(){return h<0&&(h=y(),E(),u((function(){setTimeout((function(){h=y(),E()}),0)}))),{get firstHiddenTime(){return h}}},C=function(t){document.prerendering?addEventListener("prerenderingchange",(function(){return t()}),!0):t()},D=[1800,3e3],w=function(t,e){e=e||{},C((function(){var n,r=b(),i=d("FCP"),a=l("paint",(function(t){t.forEach((function(t){"first-contentful-paint"===t.name&&(a.disconnect(),t.startTimee.latency){if(n)t.duration>n.latency?(n.entries=[t],n.latency=t.duration):t.duration===n.latency&&t.startTime===n.entries[0].startTime&&n.entries.push(t);else{var r={id:t.interactionId,latency:t.duration,entries:[t]};F.set(r.id,r),P.push(r)}P.sort((function(t,e){return e.latency-t.latency})),P.length>10&&P.splice(10).forEach((function(t){return F.delete(t.id)}))}}},j=function(t){var e=self.requestIdleCallback||self.setTimeout,n=-1;return t=g(t),"hidden"===document.visibilityState?t():(n=e(t),v(t)),n},q=[200,500],N=[],H=new Map,U=[],V=new WeakMap,W=new Map,z=-1,G=function(t){t.forEach((function(t){return N.push(t)}))},J=function(){W.size>10&&W.forEach((function(t,e){F.has(e)||W.delete(e)})),U=U.slice(-50);var t=new Set(U.concat(P.map((function(t){return V.get(t.entries[0])}))));H.forEach((function(e,n){t.has(n)||H.delete(n)}));var e=new Set;H.forEach((function(t){K(t.startTime,t.processingEnd).forEach((function(t){e.add(t)}))})),N=Array.from(e),z=-1};R.push((function(t){t.interactionId&&t.target&&!W.has(t.interactionId)&&W.set(t.interactionId,t.target)}),(function(t){for(var e,n=t.startTime+t.duration,r=U.length-1;r>=0;r--)if(e=U[r],Math.abs(n-e)<=8){var i=H.get(e);i.startTime=Math.min(t.startTime,i.startTime),i.processingStart=Math.min(t.processingStart,i.processingStart),i.processingEnd=Math.max(t.processingEnd,i.processingEnd),i.entries.push(t),n=e;break}n!==e&&(U.push(n),H.set(n,{startTime:t.startTime,processingStart:t.processingStart,processingEnd:t.processingEnd,entries:[t]})),(t.interactionId||"first-input"===t.entryType)&&V.set(t,n)}),(function(){z<0&&(z=j(J))}));var K=function(t,e){for(var n,r=[],i=0;n=N[i];i++)if(!(n.startTime+n.duratione)break;r.push(n)}return r},Q=function(t,e){n||(n=l("long-animation-frame",G)),function(t,e){e=e||{},C((function(){var n;x();var r,i=d("INP"),a=function(t){t.forEach(_);var e,n=(e=Math.min(P.length-1,Math.floor(O()/50)),P[e]);n&&n.latency!==i.value&&(i.value=n.latency,i.entries=n.entries,r())},o=l("event",a,{durationThreshold:null!==(n=e.durationThreshold)&&void 0!==n?n:40});r=m(t,i,q,e.reportAllChanges),o&&("PerformanceEventTiming"in self&&"interactionId"in PerformanceEventTiming.prototype&&o.observe({type:"first-input",buffered:!0}),v((function(){a(o.takeRecords()),r(!0)})),u((function(){B=0,P.length=0,F.clear(),i=d("INP"),r=m(t,i,q,e.reportAllChanges)})))}))}((function(e){j((function(){var n=function(t){var e=t.entries[0],n=V.get(e),r=H.get(n),a=e.processingStart,s=r.processingEnd,c=r.entries.sort((function(t,e){return t.processingStart-e.processingStart})),u=K(e.startTime,s),f=t.entries.find((function(t){return t.target})),d=f&&f.target||W.get(e.interactionId),l=[e.startTime+e.duration,s].concat(u.map((function(t){return t.startTime+t.duration}))),m=Math.max.apply(Math,l),p={interactionTarget:o(d),interactionTargetElement:d,interactionType:e.name.startsWith("key")?"keyboard":"pointer",interactionTime:e.startTime,nextPaintTime:m,processedEventEntries:c,longAnimationFrameEntries:u,inputDelay:a-e.startTime,processingDuration:s-a,presentationDelay:Math.max(m-s,0),loadState:i(e.startTime)};return Object.assign(t,{attribution:p})}(e);t(n)}))}),e)},X=[2500,4e3],Y={},Z=[800,1800],$=function t(e){document.prerendering?C((function(){return t(e)})):"complete"!==document.readyState?addEventListener("load",(function(){return t(e)}),!0):setTimeout(e,0)},tt=function(t,e){e=e||{};var n=d("TTFB"),i=m(t,n,Z,e.reportAllChanges);$((function(){var a=r();a&&(n.value=Math.max(a.responseStart-f(),0),n.entries=[a],i(!0),u((function(){n=d("TTFB",0),(i=m(t,n,Z,e.reportAllChanges))(!0)})))}))};new Date;function et(){!function(t,e){!function(t,e){e=e||{},w(g((function(){var n,r=d("CLS",0),i=0,a=[],o=function(t){t.forEach((function(t){if(!t.hadRecentInput){var e=a[0],n=a[a.length-1];i&&t.startTime-n.startTime<1e3&&t.startTime-e.startTime<5e3?(i+=t.value,a.push(t)):(i=t.value,a=[t])}})),i>r.value&&(r.value=i,r.entries=a,n())},s=l("layout-shift",o);s&&(n=m(t,r,L,e.reportAllChanges),v((function(){o(s.takeRecords()),n(!0)})),u((function(){i=0,r=d("CLS",0),n=m(t,r,L,e.reportAllChanges),p((function(){return n()}))})),setTimeout(n,0))})))}((function(e){var n=function(t){var e,n={};if(t.entries.length){var r=t.entries.reduce((function(t,e){return t&&t.value>e.value?t:e}));if(r&&r.sources&&r.sources.length){var a=(e=r.sources).find((function(t){return t.node&&1===t.node.nodeType}))||e[0];a&&(n={largestShiftTarget:o(a.node),largestShiftTime:r.startTime,largestShiftValue:r.value,largestShiftSource:a,largestShiftEntry:r,loadState:i(r.startTime)})}}return Object.assign(t,{attribution:n})}(e);t(n)}),e)}(t),function(t,e){w((function(e){var n=function(t){var e={timeToFirstByte:0,firstByteToFCP:t.value,loadState:i(c())};if(t.entries.length){var n=r(),a=t.entries[t.entries.length-1];if(n){var o=n.activationStart||0,s=Math.max(0,n.responseStart-o);e={timeToFirstByte:s,firstByteToFCP:t.value-s,loadState:i(t.entries[0].startTime),navigationEntry:n,fcpEntry:a}}}return Object.assign(t,{attribution:e})}(e);t(n)}),e)}(t),function(t,e){!function(t,e){e=e||{},C((function(){var n,r=b(),i=d("LCP"),a=function(t){e.reportAllChanges||(t=t.slice(-1)),t.forEach((function(t){t.startTime Date: Sun, 28 Jul 2024 17:09:19 +0100 Subject: [PATCH 3/6] GTM - Include send_to parameter to the datalayer.push --- .../web-vitals-analytics.1600384c6373dfab7bc1bf873ece5328.js | 1 + .../web-vitals-analytics.26c9e8650a743f1768edb15ad2170f0f.js | 1 - js/dist/module/web-vitals-analytics.asset.php | 2 +- js/src/send-to-analytics.js | 1 + 4 files changed, 3 insertions(+), 2 deletions(-) create mode 100644 js/dist/module/web-vitals-analytics.1600384c6373dfab7bc1bf873ece5328.js delete mode 100644 js/dist/module/web-vitals-analytics.26c9e8650a743f1768edb15ad2170f0f.js diff --git a/js/dist/module/web-vitals-analytics.1600384c6373dfab7bc1bf873ece5328.js b/js/dist/module/web-vitals-analytics.1600384c6373dfab7bc1bf873ece5328.js new file mode 100644 index 0000000..1aaadbc --- /dev/null +++ b/js/dist/module/web-vitals-analytics.1600384c6373dfab7bc1bf873ece5328.js @@ -0,0 +1 @@ +(()=>{"use strict";function t({name:t,value:e,delta:n,id:r,attribution:i,rating:a}){const o=window.webVitalsAnalyticsData?.[0]??null,s={value:n,metric_id:r,metric_value:e,metric_delta:Math.round("CLS"===t?1e3*n:n),metric_rating:a};switch(t){case"CLS":s.debug_target=i?.largestShiftTarget||"(not set)";break;case"INP":const{processingDuration:t=0,presentationDelay:e=0,interactionTarget:n="(not set)",interactionType:r="(not set)",inputDelay:a=0}=i||{},o=i?.longAnimationFrameEntries?.at(-1),c=o?.scripts?.sort(((t,e)=>e.duration-t.duration))[0],u={inputDelay:a,processingDuration:t,presentationDelay:e};s.processingDuration=Math.round(t),s.presentationDelay=Math.round(e),s.debug_target=n,s.interactionType=r;const f=Object.keys(u).reduce(((t,e)=>u[t]>u[e]?t:e));if(u[f]>50&&(s.maxDelay=f),c){const{invokerType:t="(not set)",invoker:e="(not set)",sourceURL:n="(not set)",sourceCharPosition:r=0,sourceFunctionName:i="(not set)"}=c,{startTime:a=0,duration:u=0,styleAndLayoutStart:f=0}=o||{},d=a+u-f;s.invokerType=t,s.invoker=e,s.sourceURL=n,s.sourceCharPosition=r,s.sourceFunctionName=i,s.styleLayoutDuration=Math.round(d)}break;case"LCP":s.debug_target=i?.element||"(not set)";break;default:return"(not set)"}o&&o.ga4_id&&("function"==typeof gtag?gtag("event",t,s):window.dataLayer&&"function"==typeof window.dataLayer.push?window.dataLayer.push({event:t,send_to:o.ga4_id,...s}):console.log("Event:",t,s))}var e,n,r=function(){var t=self.performance&&performance.getEntriesByType&&performance.getEntriesByType("navigation")[0];if(t&&t.responseStart>0&&t.responseStart(e||100)-1)return n||i;if(n=n?i+">"+n:i,r.id)break;t=r.parentNode}}catch(t){}return n},s=-1,c=function(){return s},u=function(t){addEventListener("pageshow",(function(e){e.persisted&&(s=e.timeStamp,t(e))}),!0)},f=function(){var t=r();return t&&t.activationStart||0},d=function(t,e){var n=r(),i="navigate";return c()>=0?i="back-forward-cache":n&&(document.prerendering||f()>0?i="prerender":document.wasDiscarded?i="restore":n.type&&(i=n.type.replace(/_/g,"-"))),{name:t,value:void 0===e?-1:e,rating:"good",delta:0,entries:[],id:"v4-".concat(Date.now(),"-").concat(Math.floor(8999999999999*Math.random())+1e12),navigationType:i}},l=function(t,e,n){try{if(PerformanceObserver.supportedEntryTypes.includes(t)){var r=new PerformanceObserver((function(t){Promise.resolve().then((function(){e(t.getEntries())}))}));return r.observe(Object.assign({type:t,buffered:!0},n||{})),r}}catch(t){}},m=function(t,e,n,r){var i,a;return function(o){e.value>=0&&(o||r)&&((a=e.value-(i||0))||void 0===i)&&(i=e.value,e.delta=a,e.rating=function(t,e){return t>e[1]?"poor":t>e[0]?"needs-improvement":"good"}(e.value,n),t(e))}},p=function(t){requestAnimationFrame((function(){return requestAnimationFrame((function(){return t()}))}))},v=function(t){document.addEventListener("visibilitychange",(function(){"hidden"===document.visibilityState&&t()}))},g=function(t){var e=!1;return function(){e||(t(),e=!0)}},h=-1,y=function(){return"hidden"!==document.visibilityState||document.prerendering?1/0:0},T=function(t){"hidden"===document.visibilityState&&h>-1&&(h="visibilitychange"===t.type?t.timeStamp:0,S())},E=function(){addEventListener("visibilitychange",T,!0),addEventListener("prerenderingchange",T,!0)},S=function(){removeEventListener("visibilitychange",T,!0),removeEventListener("prerenderingchange",T,!0)},b=function(){return h<0&&(h=y(),E(),u((function(){setTimeout((function(){h=y(),E()}),0)}))),{get firstHiddenTime(){return h}}},w=function(t){document.prerendering?addEventListener("prerenderingchange",(function(){return t()}),!0):t()},L=[1800,3e3],C=function(t,e){e=e||{},w((function(){var n,r=b(),i=d("FCP"),a=l("paint",(function(t){t.forEach((function(t){"first-contentful-paint"===t.name&&(a.disconnect(),t.startTimee.latency){if(n)t.duration>n.latency?(n.entries=[t],n.latency=t.duration):t.duration===n.latency&&t.startTime===n.entries[0].startTime&&n.entries.push(t);else{var r={id:t.interactionId,latency:t.duration,entries:[t]};F.set(r.id,r),P.push(r)}P.sort((function(t,e){return e.latency-t.latency})),P.length>10&&P.splice(10).forEach((function(t){return F.delete(t.id)}))}}},j=function(t){var e=self.requestIdleCallback||self.setTimeout,n=-1;return t=g(t),"hidden"===document.visibilityState?t():(n=e(t),v(t)),n},q=[200,500],N=[],H=new Map,U=[],V=new WeakMap,W=new Map,z=-1,G=function(t){t.forEach((function(t){return N.push(t)}))},J=function(){W.size>10&&W.forEach((function(t,e){F.has(e)||W.delete(e)})),U=U.slice(-50);var t=new Set(U.concat(P.map((function(t){return V.get(t.entries[0])}))));H.forEach((function(e,n){t.has(n)||H.delete(n)}));var e=new Set;H.forEach((function(t){K(t.startTime,t.processingEnd).forEach((function(t){e.add(t)}))})),N=Array.from(e),z=-1};O.push((function(t){t.interactionId&&t.target&&!W.has(t.interactionId)&&W.set(t.interactionId,t.target)}),(function(t){for(var e,n=t.startTime+t.duration,r=U.length-1;r>=0;r--)if(e=U[r],Math.abs(n-e)<=8){var i=H.get(e);i.startTime=Math.min(t.startTime,i.startTime),i.processingStart=Math.min(t.processingStart,i.processingStart),i.processingEnd=Math.max(t.processingEnd,i.processingEnd),i.entries.push(t),n=e;break}n!==e&&(U.push(n),H.set(n,{startTime:t.startTime,processingStart:t.processingStart,processingEnd:t.processingEnd,entries:[t]})),(t.interactionId||"first-input"===t.entryType)&&V.set(t,n)}),(function(){z<0&&(z=j(J))}));var K=function(t,e){for(var n,r=[],i=0;n=N[i];i++)if(!(n.startTime+n.duratione)break;r.push(n)}return r},Q=function(t,e){n||(n=l("long-animation-frame",G)),function(t,e){e=e||{},w((function(){var n;x();var r,i=d("INP"),a=function(t){t.forEach(R);var e,n=(e=Math.min(P.length-1,Math.floor(_()/50)),P[e]);n&&n.latency!==i.value&&(i.value=n.latency,i.entries=n.entries,r())},o=l("event",a,{durationThreshold:null!==(n=e.durationThreshold)&&void 0!==n?n:40});r=m(t,i,q,e.reportAllChanges),o&&("PerformanceEventTiming"in self&&"interactionId"in PerformanceEventTiming.prototype&&o.observe({type:"first-input",buffered:!0}),v((function(){a(o.takeRecords()),r(!0)})),u((function(){B=0,P.length=0,F.clear(),i=d("INP"),r=m(t,i,q,e.reportAllChanges)})))}))}((function(e){j((function(){var n=function(t){var e=t.entries[0],n=V.get(e),r=H.get(n),a=e.processingStart,s=r.processingEnd,c=r.entries.sort((function(t,e){return t.processingStart-e.processingStart})),u=K(e.startTime,s),f=t.entries.find((function(t){return t.target})),d=f&&f.target||W.get(e.interactionId),l=[e.startTime+e.duration,s].concat(u.map((function(t){return t.startTime+t.duration}))),m=Math.max.apply(Math,l),p={interactionTarget:o(d),interactionTargetElement:d,interactionType:e.name.startsWith("key")?"keyboard":"pointer",interactionTime:e.startTime,nextPaintTime:m,processedEventEntries:c,longAnimationFrameEntries:u,inputDelay:a-e.startTime,processingDuration:s-a,presentationDelay:Math.max(m-s,0),loadState:i(e.startTime)};return Object.assign(t,{attribution:p})}(e);t(n)}))}),e)},X=[2500,4e3],Y={},Z=[800,1800],$=function t(e){document.prerendering?w((function(){return t(e)})):"complete"!==document.readyState?addEventListener("load",(function(){return t(e)}),!0):setTimeout(e,0)},tt=function(t,e){e=e||{};var n=d("TTFB"),i=m(t,n,Z,e.reportAllChanges);$((function(){var a=r();a&&(n.value=Math.max(a.responseStart-f(),0),n.entries=[a],i(!0),u((function(){n=d("TTFB",0),(i=m(t,n,Z,e.reportAllChanges))(!0)})))}))};new Date;function et(){!function(t,e){!function(t,e){e=e||{},C(g((function(){var n,r=d("CLS",0),i=0,a=[],o=function(t){t.forEach((function(t){if(!t.hadRecentInput){var e=a[0],n=a[a.length-1];i&&t.startTime-n.startTime<1e3&&t.startTime-e.startTime<5e3?(i+=t.value,a.push(t)):(i=t.value,a=[t])}})),i>r.value&&(r.value=i,r.entries=a,n())},s=l("layout-shift",o);s&&(n=m(t,r,D,e.reportAllChanges),v((function(){o(s.takeRecords()),n(!0)})),u((function(){i=0,r=d("CLS",0),n=m(t,r,D,e.reportAllChanges),p((function(){return n()}))})),setTimeout(n,0))})))}((function(e){var n=function(t){var e,n={};if(t.entries.length){var r=t.entries.reduce((function(t,e){return t&&t.value>e.value?t:e}));if(r&&r.sources&&r.sources.length){var a=(e=r.sources).find((function(t){return t.node&&1===t.node.nodeType}))||e[0];a&&(n={largestShiftTarget:o(a.node),largestShiftTime:r.startTime,largestShiftValue:r.value,largestShiftSource:a,largestShiftEntry:r,loadState:i(r.startTime)})}}return Object.assign(t,{attribution:n})}(e);t(n)}),e)}(t),function(t,e){C((function(e){var n=function(t){var e={timeToFirstByte:0,firstByteToFCP:t.value,loadState:i(c())};if(t.entries.length){var n=r(),a=t.entries[t.entries.length-1];if(n){var o=n.activationStart||0,s=Math.max(0,n.responseStart-o);e={timeToFirstByte:s,firstByteToFCP:t.value-s,loadState:i(t.entries[0].startTime),navigationEntry:n,fcpEntry:a}}}return Object.assign(t,{attribution:e})}(e);t(n)}),e)}(t),function(t,e){!function(t,e){e=e||{},w((function(){var n,r=b(),i=d("LCP"),a=function(t){e.reportAllChanges||(t=t.slice(-1)),t.forEach((function(t){t.startTime{"use strict";function t({name:t,value:e,delta:n,id:r,attribution:i,rating:a}){const o=window.webVitalsAnalyticsData?.[0]??null,s={value:n,metric_id:r,metric_value:e,metric_delta:Math.round("CLS"===t?1e3*n:n),metric_rating:a};switch(t){case"CLS":s.debug_target=i?.largestShiftTarget||"(not set)";break;case"INP":const{processingDuration:t=0,presentationDelay:e=0,interactionTarget:n="(not set)",interactionType:r="(not set)",inputDelay:a=0}=i||{},o=i?.longAnimationFrameEntries?.at(-1),c=o?.scripts?.sort(((t,e)=>e.duration-t.duration))[0],u={inputDelay:a,processingDuration:t,presentationDelay:e};s.processingDuration=Math.round(t),s.presentationDelay=Math.round(e),s.debug_target=n,s.interactionType=r;const f=Object.keys(u).reduce(((t,e)=>u[t]>u[e]?t:e));if(u[f]>50&&(s.maxDelay=f),c){const{invokerType:t="(not set)",invoker:e="(not set)",sourceURL:n="(not set)",sourceCharPosition:r=0,sourceFunctionName:i="(not set)"}=c,{startTime:a=0,duration:u=0,styleAndLayoutStart:f=0}=o||{},d=a+u-f;s.invokerType=t,s.invoker=e,s.sourceURL=n,s.sourceCharPosition=r,s.sourceFunctionName=i,s.styleLayoutDuration=Math.round(d)}break;case"LCP":s.debug_target=i?.element||"(not set)";break;default:return"(not set)"}o&&o.ga4_id&&("function"==typeof gtag?gtag("event",t,s):window.dataLayer&&"function"==typeof window.dataLayer.push?window.dataLayer.push({event:t,...s}):console.log("Event:",t,s))}var e,n,r=function(){var t=self.performance&&performance.getEntriesByType&&performance.getEntriesByType("navigation")[0];if(t&&t.responseStart>0&&t.responseStart(e||100)-1)return n||i;if(n=n?i+">"+n:i,r.id)break;t=r.parentNode}}catch(t){}return n},s=-1,c=function(){return s},u=function(t){addEventListener("pageshow",(function(e){e.persisted&&(s=e.timeStamp,t(e))}),!0)},f=function(){var t=r();return t&&t.activationStart||0},d=function(t,e){var n=r(),i="navigate";return c()>=0?i="back-forward-cache":n&&(document.prerendering||f()>0?i="prerender":document.wasDiscarded?i="restore":n.type&&(i=n.type.replace(/_/g,"-"))),{name:t,value:void 0===e?-1:e,rating:"good",delta:0,entries:[],id:"v4-".concat(Date.now(),"-").concat(Math.floor(8999999999999*Math.random())+1e12),navigationType:i}},l=function(t,e,n){try{if(PerformanceObserver.supportedEntryTypes.includes(t)){var r=new PerformanceObserver((function(t){Promise.resolve().then((function(){e(t.getEntries())}))}));return r.observe(Object.assign({type:t,buffered:!0},n||{})),r}}catch(t){}},m=function(t,e,n,r){var i,a;return function(o){e.value>=0&&(o||r)&&((a=e.value-(i||0))||void 0===i)&&(i=e.value,e.delta=a,e.rating=function(t,e){return t>e[1]?"poor":t>e[0]?"needs-improvement":"good"}(e.value,n),t(e))}},p=function(t){requestAnimationFrame((function(){return requestAnimationFrame((function(){return t()}))}))},v=function(t){document.addEventListener("visibilitychange",(function(){"hidden"===document.visibilityState&&t()}))},g=function(t){var e=!1;return function(){e||(t(),e=!0)}},h=-1,y=function(){return"hidden"!==document.visibilityState||document.prerendering?1/0:0},T=function(t){"hidden"===document.visibilityState&&h>-1&&(h="visibilitychange"===t.type?t.timeStamp:0,S())},E=function(){addEventListener("visibilitychange",T,!0),addEventListener("prerenderingchange",T,!0)},S=function(){removeEventListener("visibilitychange",T,!0),removeEventListener("prerenderingchange",T,!0)},b=function(){return h<0&&(h=y(),E(),u((function(){setTimeout((function(){h=y(),E()}),0)}))),{get firstHiddenTime(){return h}}},w=function(t){document.prerendering?addEventListener("prerenderingchange",(function(){return t()}),!0):t()},L=[1800,3e3],C=function(t,e){e=e||{},w((function(){var n,r=b(),i=d("FCP"),a=l("paint",(function(t){t.forEach((function(t){"first-contentful-paint"===t.name&&(a.disconnect(),t.startTimee.latency){if(n)t.duration>n.latency?(n.entries=[t],n.latency=t.duration):t.duration===n.latency&&t.startTime===n.entries[0].startTime&&n.entries.push(t);else{var r={id:t.interactionId,latency:t.duration,entries:[t]};F.set(r.id,r),P.push(r)}P.sort((function(t,e){return e.latency-t.latency})),P.length>10&&P.splice(10).forEach((function(t){return F.delete(t.id)}))}}},j=function(t){var e=self.requestIdleCallback||self.setTimeout,n=-1;return t=g(t),"hidden"===document.visibilityState?t():(n=e(t),v(t)),n},q=[200,500],N=[],H=new Map,U=[],V=new WeakMap,W=new Map,z=-1,G=function(t){t.forEach((function(t){return N.push(t)}))},J=function(){W.size>10&&W.forEach((function(t,e){F.has(e)||W.delete(e)})),U=U.slice(-50);var t=new Set(U.concat(P.map((function(t){return V.get(t.entries[0])}))));H.forEach((function(e,n){t.has(n)||H.delete(n)}));var e=new Set;H.forEach((function(t){K(t.startTime,t.processingEnd).forEach((function(t){e.add(t)}))})),N=Array.from(e),z=-1};R.push((function(t){t.interactionId&&t.target&&!W.has(t.interactionId)&&W.set(t.interactionId,t.target)}),(function(t){for(var e,n=t.startTime+t.duration,r=U.length-1;r>=0;r--)if(e=U[r],Math.abs(n-e)<=8){var i=H.get(e);i.startTime=Math.min(t.startTime,i.startTime),i.processingStart=Math.min(t.processingStart,i.processingStart),i.processingEnd=Math.max(t.processingEnd,i.processingEnd),i.entries.push(t),n=e;break}n!==e&&(U.push(n),H.set(n,{startTime:t.startTime,processingStart:t.processingStart,processingEnd:t.processingEnd,entries:[t]})),(t.interactionId||"first-input"===t.entryType)&&V.set(t,n)}),(function(){z<0&&(z=j(J))}));var K=function(t,e){for(var n,r=[],i=0;n=N[i];i++)if(!(n.startTime+n.duratione)break;r.push(n)}return r},Q=function(t,e){n||(n=l("long-animation-frame",G)),function(t,e){e=e||{},w((function(){var n;x();var r,i=d("INP"),a=function(t){t.forEach(_);var e,n=(e=Math.min(P.length-1,Math.floor(O()/50)),P[e]);n&&n.latency!==i.value&&(i.value=n.latency,i.entries=n.entries,r())},o=l("event",a,{durationThreshold:null!==(n=e.durationThreshold)&&void 0!==n?n:40});r=m(t,i,q,e.reportAllChanges),o&&("PerformanceEventTiming"in self&&"interactionId"in PerformanceEventTiming.prototype&&o.observe({type:"first-input",buffered:!0}),v((function(){a(o.takeRecords()),r(!0)})),u((function(){B=0,P.length=0,F.clear(),i=d("INP"),r=m(t,i,q,e.reportAllChanges)})))}))}((function(e){j((function(){var n=function(t){var e=t.entries[0],n=V.get(e),r=H.get(n),a=e.processingStart,s=r.processingEnd,c=r.entries.sort((function(t,e){return t.processingStart-e.processingStart})),u=K(e.startTime,s),f=t.entries.find((function(t){return t.target})),d=f&&f.target||W.get(e.interactionId),l=[e.startTime+e.duration,s].concat(u.map((function(t){return t.startTime+t.duration}))),m=Math.max.apply(Math,l),p={interactionTarget:o(d),interactionTargetElement:d,interactionType:e.name.startsWith("key")?"keyboard":"pointer",interactionTime:e.startTime,nextPaintTime:m,processedEventEntries:c,longAnimationFrameEntries:u,inputDelay:a-e.startTime,processingDuration:s-a,presentationDelay:Math.max(m-s,0),loadState:i(e.startTime)};return Object.assign(t,{attribution:p})}(e);t(n)}))}),e)},X=[2500,4e3],Y={},Z=[800,1800],$=function t(e){document.prerendering?w((function(){return t(e)})):"complete"!==document.readyState?addEventListener("load",(function(){return t(e)}),!0):setTimeout(e,0)},tt=function(t,e){e=e||{};var n=d("TTFB"),i=m(t,n,Z,e.reportAllChanges);$((function(){var a=r();a&&(n.value=Math.max(a.responseStart-f(),0),n.entries=[a],i(!0),u((function(){n=d("TTFB",0),(i=m(t,n,Z,e.reportAllChanges))(!0)})))}))};new Date;function et(){!function(t,e){!function(t,e){e=e||{},C(g((function(){var n,r=d("CLS",0),i=0,a=[],o=function(t){t.forEach((function(t){if(!t.hadRecentInput){var e=a[0],n=a[a.length-1];i&&t.startTime-n.startTime<1e3&&t.startTime-e.startTime<5e3?(i+=t.value,a.push(t)):(i=t.value,a=[t])}})),i>r.value&&(r.value=i,r.entries=a,n())},s=l("layout-shift",o);s&&(n=m(t,r,D,e.reportAllChanges),v((function(){o(s.takeRecords()),n(!0)})),u((function(){i=0,r=d("CLS",0),n=m(t,r,D,e.reportAllChanges),p((function(){return n()}))})),setTimeout(n,0))})))}((function(e){var n=function(t){var e,n={};if(t.entries.length){var r=t.entries.reduce((function(t,e){return t&&t.value>e.value?t:e}));if(r&&r.sources&&r.sources.length){var a=(e=r.sources).find((function(t){return t.node&&1===t.node.nodeType}))||e[0];a&&(n={largestShiftTarget:o(a.node),largestShiftTime:r.startTime,largestShiftValue:r.value,largestShiftSource:a,largestShiftEntry:r,loadState:i(r.startTime)})}}return Object.assign(t,{attribution:n})}(e);t(n)}),e)}(t),function(t,e){C((function(e){var n=function(t){var e={timeToFirstByte:0,firstByteToFCP:t.value,loadState:i(c())};if(t.entries.length){var n=r(),a=t.entries[t.entries.length-1];if(n){var o=n.activationStart||0,s=Math.max(0,n.responseStart-o);e={timeToFirstByte:s,firstByteToFCP:t.value-s,loadState:i(t.entries[0].startTime),navigationEntry:n,fcpEntry:a}}}return Object.assign(t,{attribution:e})}(e);t(n)}),e)}(t),function(t,e){!function(t,e){e=e||{},w((function(){var n,r=b(),i=d("LCP"),a=function(t){e.reportAllChanges||(t=t.slice(-1)),t.forEach((function(t){t.startTime array(), 'version' => '26c9e8650a743f1768edb15ad2170f0f'); + array(), 'version' => '1600384c6373dfab7bc1bf873ece5328'); diff --git a/js/src/send-to-analytics.js b/js/src/send-to-analytics.js index ac466ce..256a6bc 100644 --- a/js/src/send-to-analytics.js +++ b/js/src/send-to-analytics.js @@ -78,6 +78,7 @@ export function sendToAnalytics( { name, value, delta, id, attribution, rating } } else if ( window.dataLayer && typeof window.dataLayer.push === 'function' ) { window.dataLayer.push( { event: name, + send_to: analyticsData.ga4_id, ...eventParams, } ); } else { From 92d0f13b1e964b50b56294b27babed22002c65f9 Mon Sep 17 00:00:00 2001 From: Mike Crantea Date: Sun, 28 Jul 2024 18:22:06 +0100 Subject: [PATCH 4/6] =?UTF-8?q?Configure=20GTAG=20if=20unavailable=20as=20?= =?UTF-8?q?simply=20delivering=20to=20GTM=20isn=E2=80=99t=20sufficient?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...alytics.1600384c6373dfab7bc1bf873ece5328.js | 1 - ...alytics.665ea55752ef103c2b58645f0caf15ff.js | 1 + js/dist/module/web-vitals-analytics.asset.php | 2 +- js/src/send-to-analytics.js | 18 +++++++++++------- 4 files changed, 13 insertions(+), 9 deletions(-) delete mode 100644 js/dist/module/web-vitals-analytics.1600384c6373dfab7bc1bf873ece5328.js create mode 100644 js/dist/module/web-vitals-analytics.665ea55752ef103c2b58645f0caf15ff.js diff --git a/js/dist/module/web-vitals-analytics.1600384c6373dfab7bc1bf873ece5328.js b/js/dist/module/web-vitals-analytics.1600384c6373dfab7bc1bf873ece5328.js deleted file mode 100644 index 1aaadbc..0000000 --- a/js/dist/module/web-vitals-analytics.1600384c6373dfab7bc1bf873ece5328.js +++ /dev/null @@ -1 +0,0 @@ -(()=>{"use strict";function t({name:t,value:e,delta:n,id:r,attribution:i,rating:a}){const o=window.webVitalsAnalyticsData?.[0]??null,s={value:n,metric_id:r,metric_value:e,metric_delta:Math.round("CLS"===t?1e3*n:n),metric_rating:a};switch(t){case"CLS":s.debug_target=i?.largestShiftTarget||"(not set)";break;case"INP":const{processingDuration:t=0,presentationDelay:e=0,interactionTarget:n="(not set)",interactionType:r="(not set)",inputDelay:a=0}=i||{},o=i?.longAnimationFrameEntries?.at(-1),c=o?.scripts?.sort(((t,e)=>e.duration-t.duration))[0],u={inputDelay:a,processingDuration:t,presentationDelay:e};s.processingDuration=Math.round(t),s.presentationDelay=Math.round(e),s.debug_target=n,s.interactionType=r;const f=Object.keys(u).reduce(((t,e)=>u[t]>u[e]?t:e));if(u[f]>50&&(s.maxDelay=f),c){const{invokerType:t="(not set)",invoker:e="(not set)",sourceURL:n="(not set)",sourceCharPosition:r=0,sourceFunctionName:i="(not set)"}=c,{startTime:a=0,duration:u=0,styleAndLayoutStart:f=0}=o||{},d=a+u-f;s.invokerType=t,s.invoker=e,s.sourceURL=n,s.sourceCharPosition=r,s.sourceFunctionName=i,s.styleLayoutDuration=Math.round(d)}break;case"LCP":s.debug_target=i?.element||"(not set)";break;default:return"(not set)"}o&&o.ga4_id&&("function"==typeof gtag?gtag("event",t,s):window.dataLayer&&"function"==typeof window.dataLayer.push?window.dataLayer.push({event:t,send_to:o.ga4_id,...s}):console.log("Event:",t,s))}var e,n,r=function(){var t=self.performance&&performance.getEntriesByType&&performance.getEntriesByType("navigation")[0];if(t&&t.responseStart>0&&t.responseStart(e||100)-1)return n||i;if(n=n?i+">"+n:i,r.id)break;t=r.parentNode}}catch(t){}return n},s=-1,c=function(){return s},u=function(t){addEventListener("pageshow",(function(e){e.persisted&&(s=e.timeStamp,t(e))}),!0)},f=function(){var t=r();return t&&t.activationStart||0},d=function(t,e){var n=r(),i="navigate";return c()>=0?i="back-forward-cache":n&&(document.prerendering||f()>0?i="prerender":document.wasDiscarded?i="restore":n.type&&(i=n.type.replace(/_/g,"-"))),{name:t,value:void 0===e?-1:e,rating:"good",delta:0,entries:[],id:"v4-".concat(Date.now(),"-").concat(Math.floor(8999999999999*Math.random())+1e12),navigationType:i}},l=function(t,e,n){try{if(PerformanceObserver.supportedEntryTypes.includes(t)){var r=new PerformanceObserver((function(t){Promise.resolve().then((function(){e(t.getEntries())}))}));return r.observe(Object.assign({type:t,buffered:!0},n||{})),r}}catch(t){}},m=function(t,e,n,r){var i,a;return function(o){e.value>=0&&(o||r)&&((a=e.value-(i||0))||void 0===i)&&(i=e.value,e.delta=a,e.rating=function(t,e){return t>e[1]?"poor":t>e[0]?"needs-improvement":"good"}(e.value,n),t(e))}},p=function(t){requestAnimationFrame((function(){return requestAnimationFrame((function(){return t()}))}))},v=function(t){document.addEventListener("visibilitychange",(function(){"hidden"===document.visibilityState&&t()}))},g=function(t){var e=!1;return function(){e||(t(),e=!0)}},h=-1,y=function(){return"hidden"!==document.visibilityState||document.prerendering?1/0:0},T=function(t){"hidden"===document.visibilityState&&h>-1&&(h="visibilitychange"===t.type?t.timeStamp:0,S())},E=function(){addEventListener("visibilitychange",T,!0),addEventListener("prerenderingchange",T,!0)},S=function(){removeEventListener("visibilitychange",T,!0),removeEventListener("prerenderingchange",T,!0)},b=function(){return h<0&&(h=y(),E(),u((function(){setTimeout((function(){h=y(),E()}),0)}))),{get firstHiddenTime(){return h}}},w=function(t){document.prerendering?addEventListener("prerenderingchange",(function(){return t()}),!0):t()},L=[1800,3e3],C=function(t,e){e=e||{},w((function(){var n,r=b(),i=d("FCP"),a=l("paint",(function(t){t.forEach((function(t){"first-contentful-paint"===t.name&&(a.disconnect(),t.startTimee.latency){if(n)t.duration>n.latency?(n.entries=[t],n.latency=t.duration):t.duration===n.latency&&t.startTime===n.entries[0].startTime&&n.entries.push(t);else{var r={id:t.interactionId,latency:t.duration,entries:[t]};F.set(r.id,r),P.push(r)}P.sort((function(t,e){return e.latency-t.latency})),P.length>10&&P.splice(10).forEach((function(t){return F.delete(t.id)}))}}},j=function(t){var e=self.requestIdleCallback||self.setTimeout,n=-1;return t=g(t),"hidden"===document.visibilityState?t():(n=e(t),v(t)),n},q=[200,500],N=[],H=new Map,U=[],V=new WeakMap,W=new Map,z=-1,G=function(t){t.forEach((function(t){return N.push(t)}))},J=function(){W.size>10&&W.forEach((function(t,e){F.has(e)||W.delete(e)})),U=U.slice(-50);var t=new Set(U.concat(P.map((function(t){return V.get(t.entries[0])}))));H.forEach((function(e,n){t.has(n)||H.delete(n)}));var e=new Set;H.forEach((function(t){K(t.startTime,t.processingEnd).forEach((function(t){e.add(t)}))})),N=Array.from(e),z=-1};O.push((function(t){t.interactionId&&t.target&&!W.has(t.interactionId)&&W.set(t.interactionId,t.target)}),(function(t){for(var e,n=t.startTime+t.duration,r=U.length-1;r>=0;r--)if(e=U[r],Math.abs(n-e)<=8){var i=H.get(e);i.startTime=Math.min(t.startTime,i.startTime),i.processingStart=Math.min(t.processingStart,i.processingStart),i.processingEnd=Math.max(t.processingEnd,i.processingEnd),i.entries.push(t),n=e;break}n!==e&&(U.push(n),H.set(n,{startTime:t.startTime,processingStart:t.processingStart,processingEnd:t.processingEnd,entries:[t]})),(t.interactionId||"first-input"===t.entryType)&&V.set(t,n)}),(function(){z<0&&(z=j(J))}));var K=function(t,e){for(var n,r=[],i=0;n=N[i];i++)if(!(n.startTime+n.duratione)break;r.push(n)}return r},Q=function(t,e){n||(n=l("long-animation-frame",G)),function(t,e){e=e||{},w((function(){var n;x();var r,i=d("INP"),a=function(t){t.forEach(R);var e,n=(e=Math.min(P.length-1,Math.floor(_()/50)),P[e]);n&&n.latency!==i.value&&(i.value=n.latency,i.entries=n.entries,r())},o=l("event",a,{durationThreshold:null!==(n=e.durationThreshold)&&void 0!==n?n:40});r=m(t,i,q,e.reportAllChanges),o&&("PerformanceEventTiming"in self&&"interactionId"in PerformanceEventTiming.prototype&&o.observe({type:"first-input",buffered:!0}),v((function(){a(o.takeRecords()),r(!0)})),u((function(){B=0,P.length=0,F.clear(),i=d("INP"),r=m(t,i,q,e.reportAllChanges)})))}))}((function(e){j((function(){var n=function(t){var e=t.entries[0],n=V.get(e),r=H.get(n),a=e.processingStart,s=r.processingEnd,c=r.entries.sort((function(t,e){return t.processingStart-e.processingStart})),u=K(e.startTime,s),f=t.entries.find((function(t){return t.target})),d=f&&f.target||W.get(e.interactionId),l=[e.startTime+e.duration,s].concat(u.map((function(t){return t.startTime+t.duration}))),m=Math.max.apply(Math,l),p={interactionTarget:o(d),interactionTargetElement:d,interactionType:e.name.startsWith("key")?"keyboard":"pointer",interactionTime:e.startTime,nextPaintTime:m,processedEventEntries:c,longAnimationFrameEntries:u,inputDelay:a-e.startTime,processingDuration:s-a,presentationDelay:Math.max(m-s,0),loadState:i(e.startTime)};return Object.assign(t,{attribution:p})}(e);t(n)}))}),e)},X=[2500,4e3],Y={},Z=[800,1800],$=function t(e){document.prerendering?w((function(){return t(e)})):"complete"!==document.readyState?addEventListener("load",(function(){return t(e)}),!0):setTimeout(e,0)},tt=function(t,e){e=e||{};var n=d("TTFB"),i=m(t,n,Z,e.reportAllChanges);$((function(){var a=r();a&&(n.value=Math.max(a.responseStart-f(),0),n.entries=[a],i(!0),u((function(){n=d("TTFB",0),(i=m(t,n,Z,e.reportAllChanges))(!0)})))}))};new Date;function et(){!function(t,e){!function(t,e){e=e||{},C(g((function(){var n,r=d("CLS",0),i=0,a=[],o=function(t){t.forEach((function(t){if(!t.hadRecentInput){var e=a[0],n=a[a.length-1];i&&t.startTime-n.startTime<1e3&&t.startTime-e.startTime<5e3?(i+=t.value,a.push(t)):(i=t.value,a=[t])}})),i>r.value&&(r.value=i,r.entries=a,n())},s=l("layout-shift",o);s&&(n=m(t,r,D,e.reportAllChanges),v((function(){o(s.takeRecords()),n(!0)})),u((function(){i=0,r=d("CLS",0),n=m(t,r,D,e.reportAllChanges),p((function(){return n()}))})),setTimeout(n,0))})))}((function(e){var n=function(t){var e,n={};if(t.entries.length){var r=t.entries.reduce((function(t,e){return t&&t.value>e.value?t:e}));if(r&&r.sources&&r.sources.length){var a=(e=r.sources).find((function(t){return t.node&&1===t.node.nodeType}))||e[0];a&&(n={largestShiftTarget:o(a.node),largestShiftTime:r.startTime,largestShiftValue:r.value,largestShiftSource:a,largestShiftEntry:r,loadState:i(r.startTime)})}}return Object.assign(t,{attribution:n})}(e);t(n)}),e)}(t),function(t,e){C((function(e){var n=function(t){var e={timeToFirstByte:0,firstByteToFCP:t.value,loadState:i(c())};if(t.entries.length){var n=r(),a=t.entries[t.entries.length-1];if(n){var o=n.activationStart||0,s=Math.max(0,n.responseStart-o);e={timeToFirstByte:s,firstByteToFCP:t.value-s,loadState:i(t.entries[0].startTime),navigationEntry:n,fcpEntry:a}}}return Object.assign(t,{attribution:e})}(e);t(n)}),e)}(t),function(t,e){!function(t,e){e=e||{},w((function(){var n,r=b(),i=d("LCP"),a=function(t){e.reportAllChanges||(t=t.slice(-1)),t.forEach((function(t){t.startTime{"use strict";function t({name:t,value:e,delta:n,id:r,attribution:i,rating:a}){const o=window.webVitalsAnalyticsData?.[0]??null,c={value:n,metric_id:r,metric_value:e,metric_delta:Math.round("CLS"===t?1e3*n:n),metric_rating:a};switch(t){case"CLS":c.debug_target=i?.largestShiftTarget||"(not set)";break;case"INP":const{processingDuration:t=0,presentationDelay:e=0,interactionTarget:n="(not set)",interactionType:r="(not set)",inputDelay:a=0}=i||{},o=i?.longAnimationFrameEntries?.at(-1),s=o?.scripts?.sort(((t,e)=>e.duration-t.duration))[0],u={inputDelay:a,processingDuration:t,presentationDelay:e};c.processingDuration=Math.round(t),c.presentationDelay=Math.round(e),c.debug_target=n,c.interactionType=r;const f=Object.keys(u).reduce(((t,e)=>u[t]>u[e]?t:e));if(u[f]>50&&(c.maxDelay=f),s){const{invokerType:t="(not set)",invoker:e="(not set)",sourceURL:n="(not set)",sourceCharPosition:r=0,sourceFunctionName:i="(not set)"}=s,{startTime:a=0,duration:u=0,styleAndLayoutStart:f=0}=o||{},d=a+u-f;c.invokerType=t,c.invoker=e,c.sourceURL=n,c.sourceCharPosition=r,c.sourceFunctionName=i,c.styleLayoutDuration=Math.round(d)}break;case"LCP":c.debug_target=i?.element||"(not set)";break;default:return"(not set)"}o&&o.ga4_id&&("function"!=typeof window.gtag&&window.dataLayer&&"function"==typeof window.dataLayer.push&&(window.gtag=function(){window.dataLayer.push(arguments)},window.gtag("config",o.ga4_id,{send_page_view:!1})),"function"==typeof window.gtag?window.gtag("event",t,c):console.log("Event:",t,c))}var e,n,r=function(){var t=self.performance&&performance.getEntriesByType&&performance.getEntriesByType("navigation")[0];if(t&&t.responseStart>0&&t.responseStart(e||100)-1)return n||i;if(n=n?i+">"+n:i,r.id)break;t=r.parentNode}}catch(t){}return n},c=-1,s=function(){return c},u=function(t){addEventListener("pageshow",(function(e){e.persisted&&(c=e.timeStamp,t(e))}),!0)},f=function(){var t=r();return t&&t.activationStart||0},d=function(t,e){var n=r(),i="navigate";return s()>=0?i="back-forward-cache":n&&(document.prerendering||f()>0?i="prerender":document.wasDiscarded?i="restore":n.type&&(i=n.type.replace(/_/g,"-"))),{name:t,value:void 0===e?-1:e,rating:"good",delta:0,entries:[],id:"v4-".concat(Date.now(),"-").concat(Math.floor(8999999999999*Math.random())+1e12),navigationType:i}},l=function(t,e,n){try{if(PerformanceObserver.supportedEntryTypes.includes(t)){var r=new PerformanceObserver((function(t){Promise.resolve().then((function(){e(t.getEntries())}))}));return r.observe(Object.assign({type:t,buffered:!0},n||{})),r}}catch(t){}},m=function(t,e,n,r){var i,a;return function(o){e.value>=0&&(o||r)&&((a=e.value-(i||0))||void 0===i)&&(i=e.value,e.delta=a,e.rating=function(t,e){return t>e[1]?"poor":t>e[0]?"needs-improvement":"good"}(e.value,n),t(e))}},p=function(t){requestAnimationFrame((function(){return requestAnimationFrame((function(){return t()}))}))},g=function(t){document.addEventListener("visibilitychange",(function(){"hidden"===document.visibilityState&&t()}))},v=function(t){var e=!1;return function(){e||(t(),e=!0)}},h=-1,y=function(){return"hidden"!==document.visibilityState||document.prerendering?1/0:0},T=function(t){"hidden"===document.visibilityState&&h>-1&&(h="visibilitychange"===t.type?t.timeStamp:0,S())},E=function(){addEventListener("visibilitychange",T,!0),addEventListener("prerenderingchange",T,!0)},S=function(){removeEventListener("visibilitychange",T,!0),removeEventListener("prerenderingchange",T,!0)},w=function(){return h<0&&(h=y(),E(),u((function(){setTimeout((function(){h=y(),E()}),0)}))),{get firstHiddenTime(){return h}}},b=function(t){document.prerendering?addEventListener("prerenderingchange",(function(){return t()}),!0):t()},L=[1800,3e3],C=function(t,e){e=e||{},b((function(){var n,r=w(),i=d("FCP"),a=l("paint",(function(t){t.forEach((function(t){"first-contentful-paint"===t.name&&(a.disconnect(),t.startTimee.latency){if(n)t.duration>n.latency?(n.entries=[t],n.latency=t.duration):t.duration===n.latency&&t.startTime===n.entries[0].startTime&&n.entries.push(t);else{var r={id:t.interactionId,latency:t.duration,entries:[t]};F.set(r.id,r),P.push(r)}P.sort((function(t,e){return e.latency-t.latency})),P.length>10&&P.splice(10).forEach((function(t){return F.delete(t.id)}))}}},j=function(t){var e=self.requestIdleCallback||self.setTimeout,n=-1;return t=v(t),"hidden"===document.visibilityState?t():(n=e(t),g(t)),n},q=[200,500],N=[],H=new Map,U=[],V=new WeakMap,W=new Map,z=-1,G=function(t){t.forEach((function(t){return N.push(t)}))},J=function(){W.size>10&&W.forEach((function(t,e){F.has(e)||W.delete(e)})),U=U.slice(-50);var t=new Set(U.concat(P.map((function(t){return V.get(t.entries[0])}))));H.forEach((function(e,n){t.has(n)||H.delete(n)}));var e=new Set;H.forEach((function(t){K(t.startTime,t.processingEnd).forEach((function(t){e.add(t)}))})),N=Array.from(e),z=-1};O.push((function(t){t.interactionId&&t.target&&!W.has(t.interactionId)&&W.set(t.interactionId,t.target)}),(function(t){for(var e,n=t.startTime+t.duration,r=U.length-1;r>=0;r--)if(e=U[r],Math.abs(n-e)<=8){var i=H.get(e);i.startTime=Math.min(t.startTime,i.startTime),i.processingStart=Math.min(t.processingStart,i.processingStart),i.processingEnd=Math.max(t.processingEnd,i.processingEnd),i.entries.push(t),n=e;break}n!==e&&(U.push(n),H.set(n,{startTime:t.startTime,processingStart:t.processingStart,processingEnd:t.processingEnd,entries:[t]})),(t.interactionId||"first-input"===t.entryType)&&V.set(t,n)}),(function(){z<0&&(z=j(J))}));var K=function(t,e){for(var n,r=[],i=0;n=N[i];i++)if(!(n.startTime+n.duratione)break;r.push(n)}return r},Q=function(t,e){n||(n=l("long-animation-frame",G)),function(t,e){e=e||{},b((function(){var n;x();var r,i=d("INP"),a=function(t){t.forEach(R);var e,n=(e=Math.min(P.length-1,Math.floor(B()/50)),P[e]);n&&n.latency!==i.value&&(i.value=n.latency,i.entries=n.entries,r())},o=l("event",a,{durationThreshold:null!==(n=e.durationThreshold)&&void 0!==n?n:40});r=m(t,i,q,e.reportAllChanges),o&&("PerformanceEventTiming"in self&&"interactionId"in PerformanceEventTiming.prototype&&o.observe({type:"first-input",buffered:!0}),g((function(){a(o.takeRecords()),r(!0)})),u((function(){_=0,P.length=0,F.clear(),i=d("INP"),r=m(t,i,q,e.reportAllChanges)})))}))}((function(e){j((function(){var n=function(t){var e=t.entries[0],n=V.get(e),r=H.get(n),a=e.processingStart,c=r.processingEnd,s=r.entries.sort((function(t,e){return t.processingStart-e.processingStart})),u=K(e.startTime,c),f=t.entries.find((function(t){return t.target})),d=f&&f.target||W.get(e.interactionId),l=[e.startTime+e.duration,c].concat(u.map((function(t){return t.startTime+t.duration}))),m=Math.max.apply(Math,l),p={interactionTarget:o(d),interactionTargetElement:d,interactionType:e.name.startsWith("key")?"keyboard":"pointer",interactionTime:e.startTime,nextPaintTime:m,processedEventEntries:s,longAnimationFrameEntries:u,inputDelay:a-e.startTime,processingDuration:c-a,presentationDelay:Math.max(m-c,0),loadState:i(e.startTime)};return Object.assign(t,{attribution:p})}(e);t(n)}))}),e)},X=[2500,4e3],Y={},Z=[800,1800],$=function t(e){document.prerendering?b((function(){return t(e)})):"complete"!==document.readyState?addEventListener("load",(function(){return t(e)}),!0):setTimeout(e,0)},tt=function(t,e){e=e||{};var n=d("TTFB"),i=m(t,n,Z,e.reportAllChanges);$((function(){var a=r();a&&(n.value=Math.max(a.responseStart-f(),0),n.entries=[a],i(!0),u((function(){n=d("TTFB",0),(i=m(t,n,Z,e.reportAllChanges))(!0)})))}))};new Date;function et(){!function(t,e){!function(t,e){e=e||{},C(v((function(){var n,r=d("CLS",0),i=0,a=[],o=function(t){t.forEach((function(t){if(!t.hadRecentInput){var e=a[0],n=a[a.length-1];i&&t.startTime-n.startTime<1e3&&t.startTime-e.startTime<5e3?(i+=t.value,a.push(t)):(i=t.value,a=[t])}})),i>r.value&&(r.value=i,r.entries=a,n())},c=l("layout-shift",o);c&&(n=m(t,r,D,e.reportAllChanges),g((function(){o(c.takeRecords()),n(!0)})),u((function(){i=0,r=d("CLS",0),n=m(t,r,D,e.reportAllChanges),p((function(){return n()}))})),setTimeout(n,0))})))}((function(e){var n=function(t){var e,n={};if(t.entries.length){var r=t.entries.reduce((function(t,e){return t&&t.value>e.value?t:e}));if(r&&r.sources&&r.sources.length){var a=(e=r.sources).find((function(t){return t.node&&1===t.node.nodeType}))||e[0];a&&(n={largestShiftTarget:o(a.node),largestShiftTime:r.startTime,largestShiftValue:r.value,largestShiftSource:a,largestShiftEntry:r,loadState:i(r.startTime)})}}return Object.assign(t,{attribution:n})}(e);t(n)}),e)}(t),function(t,e){C((function(e){var n=function(t){var e={timeToFirstByte:0,firstByteToFCP:t.value,loadState:i(s())};if(t.entries.length){var n=r(),a=t.entries[t.entries.length-1];if(n){var o=n.activationStart||0,c=Math.max(0,n.responseStart-o);e={timeToFirstByte:c,firstByteToFCP:t.value-c,loadState:i(t.entries[0].startTime),navigationEntry:n,fcpEntry:a}}}return Object.assign(t,{attribution:e})}(e);t(n)}),e)}(t),function(t,e){!function(t,e){e=e||{},b((function(){var n,r=w(),i=d("LCP"),a=function(t){e.reportAllChanges||(t=t.slice(-1)),t.forEach((function(t){t.startTime array(), 'version' => '1600384c6373dfab7bc1bf873ece5328'); + array(), 'version' => '665ea55752ef103c2b58645f0caf15ff'); diff --git a/js/src/send-to-analytics.js b/js/src/send-to-analytics.js index 256a6bc..1000af1 100644 --- a/js/src/send-to-analytics.js +++ b/js/src/send-to-analytics.js @@ -73,14 +73,18 @@ export function sendToAnalytics( { name, value, delta, id, attribution, rating } } if ( analyticsData && analyticsData.ga4_id ) { - if ( typeof gtag === 'function' ) { - gtag( 'event', name, eventParams ); - } else if ( window.dataLayer && typeof window.dataLayer.push === 'function' ) { - window.dataLayer.push( { - event: name, - send_to: analyticsData.ga4_id, - ...eventParams, + if ( typeof window.gtag !== 'function' && window.dataLayer && typeof window.dataLayer.push === 'function' ) { + window.gtag = function() { + window.dataLayer.push( arguments ); + }; + // We need gtag to be initialized before sending events. + window.gtag( 'config', analyticsData.ga4_id, { + send_page_view: false, } ); + } + + if ( typeof window.gtag === 'function' ) { + window.gtag( 'event', name, eventParams ); } else { // eslint-disable-next-line no-console console.log( 'Event:', name, eventParams ); From f8826c52fe3d530add54ea909ad73db5a39f4b9f Mon Sep 17 00:00:00 2001 From: Mike Crantea Date: Wed, 31 Jul 2024 11:43:21 +0100 Subject: [PATCH 5/6] Update js/src/send-to-analytics.js --- js/src/send-to-analytics.js | 1 - 1 file changed, 1 deletion(-) diff --git a/js/src/send-to-analytics.js b/js/src/send-to-analytics.js index 1000af1..b5749dd 100644 --- a/js/src/send-to-analytics.js +++ b/js/src/send-to-analytics.js @@ -14,7 +14,6 @@ * limitations under the License. */ -/* global gtag */ export function sendToAnalytics( { name, value, delta, id, attribution, rating } ) { const analyticsData = window.webVitalsAnalyticsData?.[ 0 ] ?? null; From d38f0a81f43e0897b756124183c4b592552fc73e Mon Sep 17 00:00:00 2001 From: Marco Pereirinha Date: Thu, 1 Aug 2024 10:28:05 +0100 Subject: [PATCH 6/6] Remove extra space --- js/src/send-to-analytics.js | 1 - 1 file changed, 1 deletion(-) diff --git a/js/src/send-to-analytics.js b/js/src/send-to-analytics.js index b5749dd..6860589 100644 --- a/js/src/send-to-analytics.js +++ b/js/src/send-to-analytics.js @@ -14,7 +14,6 @@ * limitations under the License. */ - export function sendToAnalytics( { name, value, delta, id, attribution, rating } ) { const analyticsData = window.webVitalsAnalyticsData?.[ 0 ] ?? null; const eventParams = {