Skip to content

Commit 8636f3f

Browse files
khushi1033rmi22186
authored andcommitted
fix: added attribute merging to logCheckoutOptionEvent (#65)
1 parent 81f0e08 commit 8636f3f

2 files changed

Lines changed: 91 additions & 0 deletions

File tree

packages/GA4Client/src/commerce-handler.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ CommerceHandler.prototype.logCheckoutOptionEvent = function (
202202
return false;
203203
}
204204

205+
ga4CommerceEventParameters = this.common.mergeObjects(
206+
ga4CommerceEventParameters,
207+
this.common.limitEventAttributes(event.EventAttributes)
208+
);
209+
205210
return this.sendCommerceEventToGA4(
206211
mapGA4EcommerceEventName(event),
207212
ga4CommerceEventParameters

packages/GA4Client/test/src/tests.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,92 @@ describe('Google Analytics 4 Event', function () {
12871287
done();
12881288
});
12891289

1290+
it('should include EventAttributes on GA4 add_shipping_info event when CheckoutOption has GA4.CommerceEventType add_shipping_info', function (done) {
1291+
mParticle.forwarder.process({
1292+
CurrencyCode: 'USD',
1293+
EventName: 'Test add_shipping_info Event',
1294+
EventDataType: MessageType.Commerce,
1295+
EventCategory: CommerceEventType.ProductCheckoutOption,
1296+
EventAttributes: {
1297+
page_url: 'https://example.com/checkout',
1298+
page_path: '/checkout',
1299+
mpID: '123',
1300+
SessionID: 'session-abc',
1301+
foo: 'bar',
1302+
},
1303+
CustomFlags: {
1304+
'GA4.CommerceEventType': 'add_shipping_info',
1305+
'GA4.ShippingTier': 'ground',
1306+
},
1307+
ProductAction: {
1308+
ProductActionType: ProductActionType.Click,
1309+
ProductList: [],
1310+
},
1311+
});
1312+
1313+
result = [
1314+
'event',
1315+
'add_shipping_info',
1316+
{
1317+
shipping_tier: 'ground',
1318+
coupon: null,
1319+
items: [],
1320+
page_url: 'https://example.com/checkout',
1321+
page_path: '/checkout',
1322+
mpID: '123',
1323+
SessionID: 'session-abc',
1324+
foo: 'bar',
1325+
send_to: 'testMeasurementId',
1326+
},
1327+
];
1328+
window.dataLayer[0].should.eql(result);
1329+
1330+
done();
1331+
});
1332+
1333+
it('should include EventAttributes on GA4 add_payment_info event when CheckoutOption has GA4.CommerceEventType add_payment_info', function (done) {
1334+
mParticle.forwarder.process({
1335+
CurrencyCode: 'USD',
1336+
EventName: 'Test add_payment_info Event',
1337+
EventDataType: MessageType.Commerce,
1338+
EventCategory: CommerceEventType.ProductCheckoutOption,
1339+
EventAttributes: {
1340+
page_url: 'https://example.com/checkout',
1341+
page_path: '/checkout',
1342+
mpID: '123',
1343+
SessionID: 'session-abc',
1344+
foo: 'bar',
1345+
},
1346+
CustomFlags: {
1347+
'GA4.CommerceEventType': 'add_payment_info',
1348+
'GA4.PaymentType': 'credit-card',
1349+
},
1350+
ProductAction: {
1351+
ProductActionType: ProductActionType.Click,
1352+
ProductList: [],
1353+
},
1354+
});
1355+
1356+
result = [
1357+
'event',
1358+
'add_payment_info',
1359+
{
1360+
payment_type: 'credit-card',
1361+
coupon: null,
1362+
items: [],
1363+
page_url: 'https://example.com/checkout',
1364+
page_path: '/checkout',
1365+
mpID: '123',
1366+
SessionID: 'session-abc',
1367+
foo: 'bar',
1368+
send_to: 'testMeasurementId',
1369+
},
1370+
];
1371+
window.dataLayer[0].should.eql(result);
1372+
1373+
done();
1374+
});
1375+
12901376
it('should log a view_cart event using ProductActionType.Unknown and a custom flag', function (done) {
12911377
mParticle.forwarder.process({
12921378
CurrencyCode: 'USD',

0 commit comments

Comments
 (0)