Skip to content

Commit

Permalink
fix: exceptional max attribute length and page_referrer flag
Browse files Browse the repository at this point in the history
  • Loading branch information
mmustafa-tse committed Apr 29, 2024
1 parent 5d856e4 commit 24a0955
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
27 changes: 26 additions & 1 deletion packages/GA4Client/src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
});
}
Expand Down
9 changes: 8 additions & 1 deletion packages/GA4Client/src/event-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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
);
Expand Down
42 changes: 42 additions & 0 deletions packages/GA4Client/test/src/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
];
Expand All @@ -1541,6 +1542,7 @@ describe('Google Analytics 4 Event', function () {
CustomFlags: {
'GA4.Title': 'Foo Page Title',
'GA4.Location': '/foo',
'GA4.Referrer': 'Foo Page Referrer'
},
});

Expand All @@ -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',
Expand Down Expand Up @@ -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',
},
];
Expand All @@ -1660,6 +1664,7 @@ describe('Google Analytics 4 Event', function () {
CustomFlags: {
'GA4.Title': 'Foo Page Title',
'GA4.Location': '/foo',
'GA4.Referrer': 'Foo Page Referrer'
},
});

Expand All @@ -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',
},
];
Expand All @@ -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 = [
Expand Down

0 comments on commit 24a0955

Please sign in to comment.