Skip to content

Commit

Permalink
fix: include measurement Id as event param
Browse files Browse the repository at this point in the history
  • Loading branch information
mmustafa-tse committed Mar 6, 2024
1 parent 4a837f0 commit 8cb4c21
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
19 changes: 9 additions & 10 deletions packages/GA4Client/src/commerce-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,10 @@ CommerceHandler.prototype.logCommerceEvent = function (event) {
null;
}

this.sendCommerceEventToGA4(
return this.sendCommerceEventToGA4(
mapGA4EcommerceEventName(event),
ga4CommerceEventParameters
);
return true;
};

CommerceHandler.prototype.sendCommerceEventToGA4 = function (
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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.',
Expand All @@ -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',
Expand All @@ -271,7 +270,7 @@ CommerceHandler.prototype.logViewCart = function (event, affiliation) {
event.ProductAction.TotalAmount ||
null;

this.sendCommerceEventToGA4(
return this.sendCommerceEventToGA4(
mapGA4EcommerceEventName(event),
ga4CommerceEventParameters
);
Expand Down
3 changes: 1 addition & 2 deletions packages/GA4Client/src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
42 changes: 29 additions & 13 deletions packages/GA4Client/test/src/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
});
Expand All @@ -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();
});
Expand All @@ -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();
});
Expand Down

0 comments on commit 8cb4c21

Please sign in to comment.