Skip to content

Commit

Permalink
Dispatch addToCart and removeFromCart events
Browse files Browse the repository at this point in the history
  • Loading branch information
italo-batista committed May 21, 2019
1 parent e8a8927 commit 3ab9f13
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

- Dispatch `addToCart` and `removeFromCart` events.

## [0.6.1] - 2019-05-17

### Fixed
Expand Down
44 changes: 43 additions & 1 deletion react/events/enhancedCommerce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface Product {
productName: string
selectedSku: string
items: Item[]
variant: string
}

const getSkuName = (selectedSku: string, items: Item[]) =>
Expand Down Expand Up @@ -60,7 +61,6 @@ export const productClick = (product: Product) => {
ga('send', 'event', {
eventAction: 'Click',
eventCategory: 'Product',
nonInteraction: 1,
})
}

Expand Down Expand Up @@ -92,6 +92,48 @@ export const productImpression = (
})
}

/** Addition to cart events
* https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#add-remove-cart
*/
export const addToCart = (
product: Product,
price: number,
quantity: number
) => {
if (!product) return

ga('ec:addProduct', {
name: product.productName,
brand: product.brand,
variant: product.variant,
price,
quantity,
})
ga('ec:setAction', 'add')
ga('send', 'event', {
eventAction: 'Click',
eventCategory: 'Add to cart',
})
}

/** Removal from cart events
* https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#add-remove-cart
*/
export const removeFromCart = (product: Product, price: number) => {
if (!product) return

ga('ec:addProduct', {
id: product.productId,
name: product.productName,
price,
})
ga('ec:setAction', 'remove')
ga('send', 'event', {
eventAction: 'Click',
eventCategory: 'Remove from cart',
})
}

/** Purchase event
* https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#measuring-transaction
*/
Expand Down
18 changes: 18 additions & 0 deletions react/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { orderPlaced } from './events/commonEvents'
import {
addToCart,
productDetail,
productClick,
productImpression,
purchase,
removeFromCart,
} from './events/enhancedCommerce'

const gaId = window.__SETTINGS__.gaId
Expand Down Expand Up @@ -92,6 +94,22 @@ function listener(e: MessageEvent) {
pageView(e.origin, e.data)
return
}

if (e.data.event === "addToCart") {
e.data.items.forEach((product: any) => {
product.productName = product.name
addToCart(product, product.price, product.quantity)
})
}

if (e.data.event === "removeFromCart") {
e.data.items.forEach((product: any) => {
product.productId = product.id
product.productName = product.name
removeFromCart(product, product.sellingPrice)
})
}

}

// Event listener for pageview
Expand Down

0 comments on commit 3ab9f13

Please sign in to comment.