From 8cb4c21540514f9a500c13b48fbdba789ffdb220 Mon Sep 17 00:00:00 2001 From: Mo Mustafa Date: Wed, 6 Mar 2024 15:25:33 -0800 Subject: [PATCH] fix: include measurement Id as event param --- packages/GA4Client/src/commerce-handler.js | 19 +++++----- packages/GA4Client/src/common.js | 3 +- packages/GA4Client/test/src/tests.js | 42 +++++++++++++++------- 3 files changed, 39 insertions(+), 25 deletions(-) diff --git a/packages/GA4Client/src/commerce-handler.js b/packages/GA4Client/src/commerce-handler.js index cf02373..4c0f825 100644 --- a/packages/GA4Client/src/commerce-handler.js +++ b/packages/GA4Client/src/commerce-handler.js @@ -136,11 +136,10 @@ CommerceHandler.prototype.logCommerceEvent = function (event) { null; } - this.sendCommerceEventToGA4( + return this.sendCommerceEventToGA4( mapGA4EcommerceEventName(event), ga4CommerceEventParameters ); - return true; }; CommerceHandler.prototype.sendCommerceEventToGA4 = function ( @@ -152,6 +151,8 @@ CommerceHandler.prototype.sendCommerceEventToGA4 = function ( } gtag('event', eventName, eventAttributes); + + return true; }; // Google previously had a CheckoutOption event, and now this has been split into 2 GA4 events - add_shipping_info and add_payment_info @@ -201,12 +202,10 @@ CommerceHandler.prototype.logCheckoutOptionEvent = function ( return false; } - this.sendCommerceEventToGA4( + return this.sendCommerceEventToGA4( mapGA4EcommerceEventName(event), ga4CommerceEventParameters ); - - return true; }; CommerceHandler.prototype.logPromotionEvent = function (event) { @@ -220,8 +219,9 @@ CommerceHandler.prototype.logPromotionEvent = function (event) { mapGA4EcommerceEventName(event), ga4CommerceEventParameters ); - return true; }); + + return true; } catch (error) { console.error( 'Error logging Promotions to GA4. Promotions not logged.', @@ -241,14 +241,13 @@ CommerceHandler.prototype.logImpressionEvent = function (event, affiliation) { affiliation ); - self.sendCommerceEventToGA4( mapGA4EcommerceEventName(event), ga4CommerceEventParameters ); - - return true; }); + + return true; } catch (error) { console.log( 'Error logging Impressions to GA4. Impressions not logged', @@ -271,7 +270,7 @@ CommerceHandler.prototype.logViewCart = function (event, affiliation) { event.ProductAction.TotalAmount || null; - this.sendCommerceEventToGA4( + return this.sendCommerceEventToGA4( mapGA4EcommerceEventName(event), ga4CommerceEventParameters ); diff --git a/packages/GA4Client/src/common.js b/packages/GA4Client/src/common.js index 6ac4fff..1c20642 100644 --- a/packages/GA4Client/src/common.js +++ b/packages/GA4Client/src/common.js @@ -7,8 +7,7 @@ var ConsentHandler = require('./consent'); var EVENT_NAME_MAX_LENGTH = 40; var EVENT_ATTRIBUTE_KEY_MAX_LENGTH = 40; var EVENT_ATTRIBUTE_VAL_MAX_LENGTH = 100; -// maximum event attributes reduced to 99 instead of 100 since now we include send_to as GA4 event parameter -var EVENT_ATTRIBUTE_MAX_NUMBER = 99; +var EVENT_ATTRIBUTE_MAX_NUMBER = 100; var USER_ATTRIBUTE_KEY_MAX_LENGTH = 24; var USER_ATTRIBUTE_VALUE_MAX_LENGTH = 36; diff --git a/packages/GA4Client/test/src/tests.js b/packages/GA4Client/test/src/tests.js index 0fe67c3..4bff02b 100644 --- a/packages/GA4Client/test/src/tests.js +++ b/packages/GA4Client/test/src/tests.js @@ -1680,7 +1680,7 @@ describe('Google Analytics 4 Event', function () { describe('limit event attributes', function () { // 101 event attribute keys because the limit is 100 - var eventAttributeKeys100 = [ + var eventAttributeKeys101 = [ 'aa', 'ab', 'ac', @@ -1781,6 +1781,7 @@ describe('Google Analytics 4 Event', function () { 'dt', 'du', 'dv', + 'dw' ]; it('should limit the number of event attribute keys', function (done) { @@ -1792,17 +1793,22 @@ describe('Google Analytics 4 Event', function () { EventAttributes: {}, }; // add on 101 event attributes - eventAttributeKeys100.forEach(function (key) { + eventAttributeKeys101.forEach(function (key) { event.EventAttributes[key] = key; }); mParticle.forwarder.process(event); var resultEventAttributeKeys = Object.keys(dataLayer[0][2]); + // confirm measurmentId as part of GA4 parameters + resultEventAttributeKeys.includes('send_to').should.equal(true); + // remove send_to to test the 100 event attribute limit since send_to is a reserved GA4 param + delete (dataLayer[0][2]).send_to + + // re-assign resultEventAttributeKeys after removing send_to from batch to only count for non-reserved params + resultEventAttributeKeys = Object.keys(dataLayer[0][2]); resultEventAttributeKeys.length.should.eql(100); // dw is the 101st item. The limit is 100, so - resultEventAttributeKeys.should.not.have.property('dv'); - // confirm measurmentId as part of GA4 parameters - resultEventAttributeKeys.should.not.have.property('send_to'); + resultEventAttributeKeys.should.not.have.property('dw'); done(); }); @@ -1825,17 +1831,22 @@ describe('Google Analytics 4 Event', function () { }; // add on 101 event attributes - eventAttributeKeys100.forEach(function (key) { + eventAttributeKeys101.forEach(function (key) { event.EventAttributes[key] = key; }); mParticle.forwarder.process(event); var resultEventAttributeKeys = Object.keys(dataLayer[0][2]); - // confirm event attribuets have been successfully set - resultEventAttributeKeys.includes('aa').should.equal(true); // confirm measurmentId as part of GA4 parameters resultEventAttributeKeys.includes('send_to').should.equal(true); + // remove send_to to test the 100 event attribute limit since send_to is a reserved GA4 param + delete (dataLayer[0][2]).send_to + + // re-assign resultEventAttributeKeys after removing send_to from batch to only count for non-reserved params + resultEventAttributeKeys = Object.keys(dataLayer[0][2]); + // confirm event attribuets have been successfully set + resultEventAttributeKeys.includes('aa').should.equal(true); // dw is the 101st item. The limit is 100, so - resultEventAttributeKeys.includes('dv').should.equal(false); + resultEventAttributeKeys.includes('dw').should.equal(false); done(); }); @@ -1854,19 +1865,24 @@ describe('Google Analytics 4 Event', function () { }; // add on 101 event attributes - eventAttributeKeys100.forEach(function (key) { + eventAttributeKeys101.forEach(function (key) { event.EventAttributes[key] = key; }); mParticle.forwarder.process(event); var resultEventAttributeKeys = Object.keys(dataLayer[0][2]); - // confirm event attribuets have been successfully set - resultEventAttributeKeys.includes('aa').should.equal(true); // confirm measurmentId as part of GA4 parameters resultEventAttributeKeys.includes('send_to').should.equal(true); + // remove send_to to test the 100 event attribute limit since send_to is a reserved GA4 param + delete (dataLayer[0][2]).send_to + + // re-assign resultEventAttributeKeys after removing send_to from batch to only count for non-reserved params + resultEventAttributeKeys = Object.keys(dataLayer[0][2]); + // confirm event attribuets have been successfully set + resultEventAttributeKeys.includes('aa').should.equal(true); // dw is the 101st item. The limit is 100, so - resultEventAttributeKeys.includes('dv').should.equal(false); + resultEventAttributeKeys.includes('dw').should.equal(false); done(); });