From 24a095561c0efe9f94d944cd0ecfb26f374d26d4 Mon Sep 17 00:00:00 2001 From: Mo Mustafa Date: Mon, 29 Apr 2024 16:28:16 -0700 Subject: [PATCH 1/2] fix: exceptional max attribute length and page_referrer flag --- packages/GA4Client/src/common.js | 27 +++++++++++++++- packages/GA4Client/src/event-handler.js | 9 +++++- packages/GA4Client/test/src/tests.js | 42 +++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 2 deletions(-) diff --git a/packages/GA4Client/src/common.js b/packages/GA4Client/src/common.js index 1c20642..fb1ad52 100644 --- a/packages/GA4Client/src/common.js +++ b/packages/GA4Client/src/common.js @@ -8,6 +8,9 @@ var EVENT_NAME_MAX_LENGTH = 40; var EVENT_ATTRIBUTE_KEY_MAX_LENGTH = 40; var EVENT_ATTRIBUTE_VAL_MAX_LENGTH = 100; var EVENT_ATTRIBUTE_MAX_NUMBER = 100; +var PAGE_TITLE_MAX_LENGTH = 300; +var PAGE_REFERRER_MAX_LENGTH = 420; +var PAGE_LOCATION_MAX_LENGTH = 1000; var USER_ATTRIBUTE_KEY_MAX_LENGTH = 24; var USER_ATTRIBUTE_VALUE_MAX_LENGTH = 36; @@ -67,7 +70,29 @@ Common.prototype.truncateAttributes = function ( if (!isEmpty(attributes)) { Object.keys(attributes).forEach(function (attribute) { var key = truncateString(attribute, keyLimit); - var val = truncateString(attributes[attribute], valueLimit); + var val; + switch (key) { + case 'page_title': + val = truncateString( + attributes[attribute], + PAGE_TITLE_MAX_LENGTH + ); + break; + case 'page_referrer': + val = truncateString( + attributes[attribute], + PAGE_REFERRER_MAX_LENGTH + ); + break; + case 'page_location': + val = truncateString( + attributes[attribute], + PAGE_LOCATION_MAX_LENGTH + ); + break; + default: + val = truncateString(attributes[attribute], valueLimit); + } truncatedAttributes[key] = val; }); } diff --git a/packages/GA4Client/src/event-handler.js b/packages/GA4Client/src/event-handler.js index 2a47939..ba0d717 100644 --- a/packages/GA4Client/src/event-handler.js +++ b/packages/GA4Client/src/event-handler.js @@ -69,13 +69,15 @@ EventHandler.prototype.logError = function () { EventHandler.prototype.logPageView = function (event) { var TITLE = 'GA4.Title'; var LOCATION = 'GA4.Location'; + var REFERRER = 'GA4.Referrer'; // These are being included for backwards compatibility from the legacy Google Analytics custom flags var LEGACY_GA_TITLE = 'Google.Title'; var LEGACY_GA_LOCATION = 'Google.Location'; var pageLocation = location.href, - pageTitle = document.title; + pageTitle = document.title, + pageReferrer = document.referrer; if (event.CustomFlags) { if (event.CustomFlags.hasOwnProperty(TITLE)) { @@ -89,12 +91,17 @@ EventHandler.prototype.logPageView = function (event) { } else if (event.CustomFlags.hasOwnProperty(LEGACY_GA_LOCATION)) { pageLocation = event.CustomFlags[LEGACY_GA_LOCATION]; } + + if (event.CustomFlags.hasOwnProperty(REFERRER)) { + pageReferrer = event.CustomFlags[REFERRER]; + } } var eventAttributes = this.common.mergeObjects( { page_title: pageTitle, page_location: pageLocation, + page_referrer: pageReferrer, }, event.EventAttributes ); diff --git a/packages/GA4Client/test/src/tests.js b/packages/GA4Client/test/src/tests.js index 5e8a457..f74b5ef 100644 --- a/packages/GA4Client/test/src/tests.js +++ b/packages/GA4Client/test/src/tests.js @@ -1522,6 +1522,7 @@ describe('Google Analytics 4 Event', function () { { page_title: 'Mocha Tests', page_location: location.href, + page_referrer: document.referrer, send_to: 'testMeasurementId', }, ]; @@ -1541,6 +1542,7 @@ describe('Google Analytics 4 Event', function () { CustomFlags: { 'GA4.Title': 'Foo Page Title', 'GA4.Location': '/foo', + 'GA4.Referrer': 'Foo Page Referrer' }, }); @@ -1550,6 +1552,7 @@ describe('Google Analytics 4 Event', function () { { page_title: 'Foo Page Title', page_location: '/foo', + page_referrer: 'Foo Page Referrer', eventKey1: 'test1', eventKey2: 'test2', send_to: 'testMeasurementId', @@ -1644,6 +1647,7 @@ describe('Google Analytics 4 Event', function () { { page_title: 'Foo Page Title', page_location: '/foo', + page_referrer: document.referrer, send_to: 'testMeasurementId', }, ]; @@ -1660,6 +1664,7 @@ describe('Google Analytics 4 Event', function () { CustomFlags: { 'GA4.Title': 'Foo Page Title', 'GA4.Location': '/foo', + 'GA4.Referrer': 'Foo Page Referrer' }, }); @@ -1669,6 +1674,7 @@ describe('Google Analytics 4 Event', function () { { page_title: 'Foo Page Title', page_location: '/foo', + page_referrer: 'Foo Page Referrer', send_to: 'testMeasurementId', }, ]; @@ -1677,6 +1683,42 @@ describe('Google Analytics 4 Event', function () { done(); }); + it('should log page view with truncated GA custom flags', function (done) { + function generateValue(length) { + var value = '' + for (let i = 0; i < length; i++) { + value += 'a' + } + return value + } + + mParticle.forwarder.process({ + EventDataType: MessageType.PageView, + EventName: 'test name', + EventAttributes: {}, + CustomFlags: { + 'GA4.Title': generateValue(305), // Max page_title length is 300 for GA4 + 'GA4.Location': generateValue(1005), // Max page_location length is 1000 for GA4 + 'GA4.Referrer': generateValue(425) // Max page_referrer length is 420 for GA4 + }, + }); + + var result = [ + 'event', + 'page_view', + { + page_title: generateValue(300), + page_location: generateValue(1000), + page_referrer: generateValue(420), + send_to: 'testMeasurementId', + }, + ]; + + window.dataLayer[0].should.eql(result); + + done(); + }) + describe('limit event attributes', function () { // 101 event attribute keys because the limit is 100 var eventAttributeKeys101 = [ From 825aaed26da08db9f09c3a4f11753d067ee13d15 Mon Sep 17 00:00:00 2001 From: Mo Mustafa Date: Thu, 2 May 2024 13:34:42 -0700 Subject: [PATCH 2/2] fix: exceptional max attribute length and page_referrer flag --- packages/GA4Client/src/common.js | 30 +++++++++++-------------- packages/GA4Client/src/event-handler.js | 9 +++++--- packages/GA4Client/test/src/tests.js | 3 ++- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/packages/GA4Client/src/common.js b/packages/GA4Client/src/common.js index fb1ad52..52a59b8 100644 --- a/packages/GA4Client/src/common.js +++ b/packages/GA4Client/src/common.js @@ -17,6 +17,10 @@ var USER_ATTRIBUTE_VALUE_MAX_LENGTH = 36; var PRODUCT_ATTRIBUTE_MAX_NUMBER = 10; +var PAGE_TITLE_KEY = 'page_title'; +var PAGE_LOCATION_KEY = 'page_location'; +var PAGE_REFERRER_KEY = 'page_referrer'; + var RESERVED_PRODUCT_KEYS = [ 'item_category', 'item_category2', @@ -70,29 +74,21 @@ Common.prototype.truncateAttributes = function ( if (!isEmpty(attributes)) { Object.keys(attributes).forEach(function (attribute) { var key = truncateString(attribute, keyLimit); - var val; + var valueLimitOverride; switch (key) { - case 'page_title': - val = truncateString( - attributes[attribute], - PAGE_TITLE_MAX_LENGTH - ); + case PAGE_TITLE_KEY: + valueLimitOverride = PAGE_TITLE_MAX_LENGTH; break; - case 'page_referrer': - val = truncateString( - attributes[attribute], - PAGE_REFERRER_MAX_LENGTH - ); + case PAGE_REFERRER_KEY: + valueLimitOverride = PAGE_REFERRER_MAX_LENGTH; break; - case 'page_location': - val = truncateString( - attributes[attribute], - PAGE_LOCATION_MAX_LENGTH - ); + case PAGE_LOCATION_KEY: + valueLimitOverride = PAGE_LOCATION_MAX_LENGTH; break; default: - val = truncateString(attributes[attribute], valueLimit); + valueLimitOverride = valueLimit; } + var val = truncateString(attributes[attribute], valueLimitOverride); truncatedAttributes[key] = val; }); } diff --git a/packages/GA4Client/src/event-handler.js b/packages/GA4Client/src/event-handler.js index ba0d717..847fafb 100644 --- a/packages/GA4Client/src/event-handler.js +++ b/packages/GA4Client/src/event-handler.js @@ -74,10 +74,11 @@ EventHandler.prototype.logPageView = function (event) { // These are being included for backwards compatibility from the legacy Google Analytics custom flags var LEGACY_GA_TITLE = 'Google.Title'; var LEGACY_GA_LOCATION = 'Google.Location'; + var LEGACY_GA_REFERRER = 'Google.DocumentReferrer'; - var pageLocation = location.href, - pageTitle = document.title, - pageReferrer = document.referrer; + var pageLocation = location.href; + var pageTitle = document.title; + var pageReferrer = document.referrer; if (event.CustomFlags) { if (event.CustomFlags.hasOwnProperty(TITLE)) { @@ -94,6 +95,8 @@ EventHandler.prototype.logPageView = function (event) { if (event.CustomFlags.hasOwnProperty(REFERRER)) { pageReferrer = event.CustomFlags[REFERRER]; + } else if (event.CustomFlags.hasOwnProperty(LEGACY_GA_REFERRER)) { + pageReferrer = event.CustomFlags[LEGACY_GA_REFERRER]; } } diff --git a/packages/GA4Client/test/src/tests.js b/packages/GA4Client/test/src/tests.js index f74b5ef..54e87c0 100644 --- a/packages/GA4Client/test/src/tests.js +++ b/packages/GA4Client/test/src/tests.js @@ -1639,6 +1639,7 @@ describe('Google Analytics 4 Event', function () { CustomFlags: { 'Google.Title': 'Foo Page Title', 'Google.Location': '/foo', + 'Google.DocumentReferrer': 'Foo Page Referrer' }, }); var result = [ @@ -1647,7 +1648,7 @@ describe('Google Analytics 4 Event', function () { { page_title: 'Foo Page Title', page_location: '/foo', - page_referrer: document.referrer, + page_referrer: 'Foo Page Referrer', send_to: 'testMeasurementId', }, ];