Skip to content

Commit

Permalink
Fix action sending for advanced ecommerce
Browse files Browse the repository at this point in the history
  • Loading branch information
italo-batista committed May 15, 2019
1 parent 88ec8be commit 4cc8c27
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
13 changes: 8 additions & 5 deletions react/events/enhancedCommerce.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { getCategory } from './../utils'

/** Product viewed event
* https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#measuring-actvities
*/

interface Item {
itemId: string
name: string
Expand All @@ -22,6 +18,9 @@ interface Product {
const getSkuName = (selectedSku: string, items: Item[]) =>
(items.find((item: Item) => selectedSku === item.itemId) || ({} as Item)).name

/** Product viewed event
* https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#measuring-actvities
*/
export const productDetail = (product: Product) => {
ga('ec:addProduct', {
brand: product.brand,
Expand Down Expand Up @@ -56,7 +55,11 @@ export const productClick = (product: Product) => {
variant: getSkuName(product.selectedSku, product.items),
})
ga('ec:setAction', 'click')
ga('send', 'event', 'productClick', 'click')
ga('send', 'event', {
eventAction: 'Click',
eventCategory: 'Product',
nonInteraction: 1,
})
}

/** Purchase event
Expand Down
12 changes: 11 additions & 1 deletion react/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { orderPlaced } from './events/commonEvents'
import { productDetail, purchase } from './events/enhancedCommerce'
import {
productDetail,
productClick,
purchase,
} from './events/enhancedCommerce'

const gaId = window.__SETTINGS__.gaId

Expand Down Expand Up @@ -47,6 +51,12 @@ window.addEventListener('message', e => {
return
}

// Event listener for productClick
if (e.data.event === 'productClick') {
productClick(e.data.product)
return
}

// Event listener for orderPlaced
if (e.data.event === 'orderPlaced') {
orderPlaced(e.data)
Expand Down

0 comments on commit 4cc8c27

Please sign in to comment.