Skip to content

Commit

Permalink
Add product types
Browse files Browse the repository at this point in the history
  • Loading branch information
italo-batista committed May 15, 2019
1 parent 7155dae commit 88ec8be
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
22 changes: 22 additions & 0 deletions react/events/enhancedCommerce.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getCategory } from './../utils'

/** Product viewed event
* https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#measuring-actvities
*/
Expand All @@ -10,6 +12,7 @@ interface Item {
interface Product {
brand: string
categoryId: string
categories: string[]
productId: string
productName: string
selectedSku: string
Expand Down Expand Up @@ -37,6 +40,25 @@ export const productDetail = (product: Product) => {
})
}

/** Product clicked event
* https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#product-click
*/
export const productClick = (product: Product) => {
if (!product) return

const category = getCategory(product.categories)

ga('ec:addProduct', {
id: product.productId,
name: product.productName,
category: category,
brand: product.brand,
variant: getSkuName(product.selectedSku, product.items),
})
ga('ec:setAction', 'click')
ga('send', 'event', 'productClick', 'click')
}

/** Purchase event
* https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#measuring-transaction
*/
Expand Down
4 changes: 2 additions & 2 deletions react/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export const getCategory = (rawCategories: any) => {
export const getCategory = (rawCategories: string[]) => {
if (!rawCategories || !rawCategories.length) return

const _filterEmpty = (string: string) => {
if (string.length > 0) return string
}

const categories = rawCategories.map(function(categoryPath: string) {
const categories = rawCategories.map(function (categoryPath: string) {
let splitedPath = categoryPath.split('/').filter(_filterEmpty)
return splitedPath[0]
})
Expand Down

0 comments on commit 88ec8be

Please sign in to comment.