Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: exceptional max attribute length and page_referrer flag addition #59

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion packages/GA4Client/src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@ 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;

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',
Expand Down Expand Up @@ -67,7 +74,21 @@ 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 valueLimitOverride;
switch (key) {
case PAGE_TITLE_KEY:
valueLimitOverride = PAGE_TITLE_MAX_LENGTH;
break;
case PAGE_REFERRER_KEY:
valueLimitOverride = PAGE_REFERRER_MAX_LENGTH;
break;
case PAGE_LOCATION_KEY:
valueLimitOverride = PAGE_LOCATION_MAX_LENGTH;
break;
default:
valueLimitOverride = valueLimit;
}
var val = truncateString(attributes[attribute], valueLimitOverride);
truncatedAttributes[key] = val;
});
}
Expand Down
14 changes: 12 additions & 2 deletions packages/GA4Client/src/event-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,16 @@ 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 LEGACY_GA_REFERRER = 'Google.DocumentReferrer';

var pageLocation = location.href,
pageTitle = document.title;
var pageLocation = location.href;
var pageTitle = document.title;
var pageReferrer = document.referrer;

if (event.CustomFlags) {
if (event.CustomFlags.hasOwnProperty(TITLE)) {
Expand All @@ -89,12 +92,19 @@ EventHandler.prototype.logPageView = function (event) {
} else if (event.CustomFlags.hasOwnProperty(LEGACY_GA_LOCATION)) {
pageLocation = event.CustomFlags[LEGACY_GA_LOCATION];
}

if (event.CustomFlags.hasOwnProperty(REFERRER)) {
mmustafa-tse marked this conversation as resolved.
Show resolved Hide resolved
pageReferrer = event.CustomFlags[REFERRER];
} else if (event.CustomFlags.hasOwnProperty(LEGACY_GA_REFERRER)) {
pageReferrer = event.CustomFlags[LEGACY_GA_REFERRER];
}
}

var eventAttributes = this.common.mergeObjects(
{
page_title: pageTitle,
page_location: pageLocation,
page_referrer: pageReferrer,
},
event.EventAttributes
);
Expand Down
43 changes: 43 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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that you added some tests that include page_referrer but are there any tests cases that reflect the behavior when it isn't included?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so as mentioned the default for page_referrer per google docs is document.referrer and I included it just how we default page_title and page_location

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 @@ -1636,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 = [
Expand All @@ -1644,6 +1648,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 @@ -1660,6 +1665,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 +1675,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 +1684,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
Loading