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

[HS-1071657] Migrate cart and view product data layer events to GA4 #1083

Merged
merged 4 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
28 changes: 13 additions & 15 deletions src/app/analytics/analytics.factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,14 @@ const analyticsFactory = /* @ngInject */ function ($window, $timeout, envService
// Send GTM Advance Ecommerce event
$window.dataLayer = $window.dataLayer || []
$window.dataLayer.push({
event: 'add-to-cart',
event: 'add_to_cart',
ecommerce: {
currencyCode: 'USD',
add: {
products: [generateProduct(productData, {
price: itemConfig.AMOUNT,
recurringDate
})]
}
value: itemConfig.AMOUNT.toFixed(2),
items: [generateProduct(productData, {
price: itemConfig.AMOUNT,
recurringDate
})]
}
})
}),
Expand All @@ -251,12 +250,11 @@ const analyticsFactory = /* @ngInject */ function ($window, $timeout, envService
// Send GTM Advance Ecommerce event
$window.dataLayer = $window.dataLayer || []
$window.dataLayer.push({
event: 'remove-from-cart',
event: 'remove_from_cart',
ecommerce: {
currencyCode: 'USD',
remove: {
products: [generateProduct(item)]
}
value: item.amount.toFixed(2),
wrandall22 marked this conversation as resolved.
Show resolved Hide resolved
items: [generateProduct(item)]
}
})
}),
Expand Down Expand Up @@ -448,12 +446,12 @@ const analyticsFactory = /* @ngInject */ function ($window, $timeout, envService
$window.digitalData.product = product
$window.dataLayer = $window.dataLayer || []
$window.dataLayer.push({
event: 'give-gift-modal',
event: 'view_item',
ecommerce: {
currencyCode: 'USD',
detail: {
products: [generateProduct(modifiedProductData)]
}
// value is unavailable until the user selects a gift amount
Copy link
Contributor

Choose a reason for hiding this comment

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

Will there ever be a view_item which has a value? If no, should we have it this object, or do the analytics guys need it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question. view_item won't ever have a value amount at this stage because the user hasn't selected the gift amount or frequency yet. When I implemented the GA4 spec for branded checkout, the spec docs said, "If no value exists for an attribute in the dataLayer, please pass JavaScript undefined as the value." That's why I'm passing undefined instead of omitting that field.

value: undefined,
items: [generateProduct(modifiedProductData)]
}
})
this.setEvent('give gift modal')
Expand Down
76 changes: 38 additions & 38 deletions src/app/analytics/analytics.factory.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('analytics factory', () => {
self.analyticsFactory = analyticsFactory
self.envService = envService
self.$window = $window
self.$window.digitalData = { cart: {} }
self.$window.dataLayer = []

self.$window.sessionStorage.clear()
Expand Down Expand Up @@ -113,8 +114,9 @@ describe('analytics factory', () => {
self.analyticsFactory.cartAdd(itemConfig, productData)

expect(self.$window.dataLayer.length).toEqual(1)
expect(self.$window.dataLayer[0].event).toEqual('add-to-cart')
expect(self.$window.dataLayer[0].ecommerce.add.products[0]).toEqual({
expect(self.$window.dataLayer[0].event).toEqual('add_to_cart')
expect(self.$window.dataLayer[0].ecommerce.value).toEqual(itemConfig.AMOUNT.toFixed(2))
expect(self.$window.dataLayer[0].ecommerce.items[0]).toEqual({
item_id: productData.code,
item_name: productData.displayName,
item_brand: productData.orgId,
Expand All @@ -134,8 +136,9 @@ describe('analytics factory', () => {
self.analyticsFactory.cartAdd(itemConfig, productData)

expect(self.$window.dataLayer.length).toEqual(1)
expect(self.$window.dataLayer[0].event).toEqual('add-to-cart')
expect(self.$window.dataLayer[0].ecommerce.add.products[0]).toEqual({
expect(self.$window.dataLayer[0].event).toEqual('add_to_cart')
expect(self.$window.dataLayer[0].ecommerce.value).toEqual(itemConfig.AMOUNT.toFixed(2))
expect(self.$window.dataLayer[0].ecommerce.items[0]).toEqual({
item_id: productData.code,
item_name: productData.displayName,
item_brand: productData.orgId,
Expand Down Expand Up @@ -260,22 +263,21 @@ describe('analytics factory', () => {
})
expect(self.$window.digitalData.cart.item.length).toEqual(1)

expect(self.$window.dataLayer[0].event).toEqual('remove-from-cart')
expect(self.$window.dataLayer[0].event).toEqual('remove_from_cart')
expect(self.$window.dataLayer[0].ecommerce).toEqual({
currencyCode: 'USD',
remove: {
products: [{
item_id: item.designationNumber,
item_name: item.displayName,
item_brand: item.orgId,
item_category: item.designationType.toLowerCase(),
item_variant: 'monthly',
currency: 'USD',
price: item.amount.toString(),
quantity: '1',
recurring_date: 'September 15, 2024'
}]
}
value: item.amount.toFixed(2),
items: [{
item_id: item.designationNumber,
item_name: item.displayName,
item_brand: item.orgId,
item_category: item.designationType.toLowerCase(),
item_variant: 'monthly',
currency: 'USD',
price: item.amount.toString(),
quantity: '1',
recurring_date: 'September 15, 2024'
}]
})
});
});
Expand Down Expand Up @@ -364,7 +366,7 @@ describe('analytics factory', () => {

it('should create payment info and checkout step DataLayer events', () => {
self.analyticsFactory.checkoutStepEvent('payment', cart)

expect(self.$window.dataLayer.length).toEqual(2)
expect(self.$window.dataLayer[0]).toEqual({
event: 'add_payment_info'
Expand All @@ -386,7 +388,7 @@ describe('analytics factory', () => {

it('should create review order and checkout step DataLayer events', () => {
self.analyticsFactory.checkoutStepEvent('review', cart)

expect(self.$window.dataLayer.length).toEqual(2)
expect(self.$window.dataLayer[0]).toEqual({
event: 'review_order',
Expand All @@ -407,7 +409,7 @@ describe('analytics factory', () => {
})
});

describe('checkoutStepOptionEvent', () => {
describe('checkoutStepOptionEvent', () => {
it('should add contact checkout option event to DataLayer', () => {
self.analyticsFactory.checkoutStepOptionEvent('Household', 'contact')
expect(self.$window.dataLayer.length).toEqual(1)
Expand Down Expand Up @@ -487,25 +489,23 @@ describe('analytics factory', () => {
"orgId": "STAFF"
}

it('should push give-gift-modal event to the DataLayer', () => {
it('should push view_item event to the DataLayer', () => {
self.analyticsFactory.giveGiftModal(productData)
expect(self.$window.dataLayer.length).toEqual(1)
expect(self.$window.dataLayer[0].event).toEqual('give-gift-modal')
expect(self.$window.dataLayer[0].event).toEqual('view_item')
expect(self.$window.dataLayer[0].ecommerce).toEqual({
currencyCode: 'USD',
detail: {
products: [{
item_id: '0643021',
item_name: 'International Staff',
item_brand: 'STAFF',
item_category: 'staff',
item_variant: undefined,
price: undefined,
currency: 'USD',
quantity: '1',
recurring_date: undefined,
}]
}
items: [{
item_id: '0643021',
item_name: 'International Staff',
item_brand: 'STAFF',
item_category: 'staff',
item_variant: undefined,
price: undefined,
currency: 'USD',
quantity: '1',
recurring_date: undefined,
}]
})
});
});
Expand Down Expand Up @@ -621,7 +621,7 @@ describe('analytics factory', () => {
self.analyticsFactory.transactionEvent(purchaseData)

expect(self.$window.sessionStorage.getItem('transactionId')).toEqual(purchaseData.rawData['purchase-number'])

expect(self.$window.dataLayer.length).toEqual(1)
expect(self.$window.dataLayer[0].event).toEqual('purchase')
expect(self.$window.dataLayer[0].paymentType).toEqual('credit card')
Expand Down Expand Up @@ -678,7 +678,7 @@ describe('analytics factory', () => {

const totalWithFees = 51.2 * 3
const totalWithoutFees = 50 * 3

expect(self.$window.dataLayer[0].ecommerce.processing_fee).toEqual((totalWithFees - totalWithoutFees).toFixed(2))
expect(self.$window.dataLayer[0].ecommerce.value).toEqual((totalWithFees).toFixed(2))
expect(self.$window.dataLayer[0].ecommerce.pays_processing).toEqual('yes')
Expand Down
Loading