Skip to content

Commit

Permalink
Merge branch 'hs-1071657-datalayer' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
canac committed Dec 20, 2023
2 parents 65b65ab + 92c61b7 commit 2b33bad
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 53 deletions.
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),
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
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

0 comments on commit 2b33bad

Please sign in to comment.