From 2e81406bdc1808b1ba8654f6895a7b70f299eea0 Mon Sep 17 00:00:00 2001 From: Anna Frankova Date: Thu, 18 Apr 2024 12:45:11 +0200 Subject: [PATCH 1/2] ga4-hit-fix Push correct format to GTM dataLayer --- packages/plugin-analytic-google/src/GoogleAnalytics4.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/plugin-analytic-google/src/GoogleAnalytics4.ts b/packages/plugin-analytic-google/src/GoogleAnalytics4.ts index d880a737..c626d5a0 100644 --- a/packages/plugin-analytic-google/src/GoogleAnalytics4.ts +++ b/packages/plugin-analytic-google/src/GoogleAnalytics4.ts @@ -171,8 +171,9 @@ export class GoogleAnalytics4 extends AbstractAnalytic { _createGlobalDefinition(window: globalThis.Window) { window.dataLayer = window.dataLayer || []; - this._ga4Script = function (...rest: unknown[]) { - window.dataLayer.push(...rest); + this._ga4Script = function (...data: unknown[]) { + // we're pushing a new object to the GoogleTagManager dataLayer + window.dataLayer.push(data); }; this._configuration(); From 7830f4b383869a14bdb7d3fe7ec08758cf272b61 Mon Sep 17 00:00:00 2001 From: Anna Frankova Date: Thu, 18 Apr 2024 13:18:52 +0200 Subject: [PATCH 2/2] ga4-hit-fix Push correct data format to GTM dataLayer --- .changeset/brown-foxes-hide.md | 9 +++++++++ packages/plugin-analytic-google/src/GoogleAnalytics4.ts | 6 +++--- 2 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 .changeset/brown-foxes-hide.md diff --git a/.changeset/brown-foxes-hide.md b/.changeset/brown-foxes-hide.md new file mode 100644 index 00000000..7312848b --- /dev/null +++ b/.changeset/brown-foxes-hide.md @@ -0,0 +1,9 @@ +--- +"@ima/plugin-analytic-google": patch +--- + +Correct GTM params + +- **What?** Push correct data format to Google Tag Manager `dataLayer`. +- **Why?** Otherwise the GA4 events are not hit. GTM needs the params in the `Arguments` array-like object - not object, not array. +- **How?** Nothing. diff --git a/packages/plugin-analytic-google/src/GoogleAnalytics4.ts b/packages/plugin-analytic-google/src/GoogleAnalytics4.ts index c626d5a0..a9678c90 100644 --- a/packages/plugin-analytic-google/src/GoogleAnalytics4.ts +++ b/packages/plugin-analytic-google/src/GoogleAnalytics4.ts @@ -171,9 +171,9 @@ export class GoogleAnalytics4 extends AbstractAnalytic { _createGlobalDefinition(window: globalThis.Window) { window.dataLayer = window.dataLayer || []; - this._ga4Script = function (...data: unknown[]) { - // we're pushing a new object to the GoogleTagManager dataLayer - window.dataLayer.push(data); + this._ga4Script = function () { + // We are passing a set of arguments to the gtag function of GTM. This MUST be an `arguments` object, not an array. + window.dataLayer.push(arguments); // eslint-disable-line prefer-rest-params }; this._configuration();