Skip to content

Commit ef68a84

Browse files
committed
Add support for GA4 setup through GTM and the window.dataLayer
1 parent c1f8765 commit ef68a84

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

js/src/send-to-analytics.js

+12-5
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
function getDeliveryFunction( type ) {
18-
// eslint-disable-next-line no-console
19-
return window[ type ] || console.log;
20-
}
17+
/* global gtag */
2118

2219
export function sendToAnalytics( { name, value, delta, id, attribution, rating } ) {
2320
const analyticsData = window.webVitalsAnalyticsData?.[ 0 ] ?? null;
@@ -76,6 +73,16 @@ export function sendToAnalytics( { name, value, delta, id, attribution, rating }
7673
}
7774

7875
if ( analyticsData && analyticsData.ga4_id ) {
79-
getDeliveryFunction( 'gtag' )( 'event', name, eventParams );
76+
if ( typeof gtag === 'function' ) {
77+
gtag( 'event', name, eventParams );
78+
} else if ( window.dataLayer && typeof window.dataLayer.push === 'function' ) {
79+
window.dataLayer.push( {
80+
event: name,
81+
...eventParams,
82+
} );
83+
} else {
84+
// eslint-disable-next-line no-console
85+
console.log( 'Event:', name, eventParams );
86+
}
8087
}
8188
}

0 commit comments

Comments
 (0)