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

Set Prices to numbers for GA4 #92

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "google-tag-manager",
"vendor": "vtex",
"version": "3.3.1",
"version": "3.3.3",
"title": "Google Tag Manager",
"description": "Google Tag Manager",
"mustUpdateAt": "2019-04-03",
Expand All @@ -13,9 +13,7 @@
"support": {
"url": "https://support.vtex.com/hc/requests"
},
"availableCountries": [
"*"
]
"availableCountries": ["*"]
},
"builders": {
"react": "3.x",
Expand Down
4 changes: 2 additions & 2 deletions react/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ test('productImpression', () => {
list: 'Shelf',
name: 'Classic Shoes Top',
position: 1,
price: '38.9',
price: 38.9,
dimension1: '12531',
dimension2: '232344',
dimension3: 'Classic Pink',
Expand All @@ -45,7 +45,7 @@ test('productImpression', () => {
list: 'Shelf',
name: 'Gorgeous Top Watch',
position: 2,
price: '2200',
price: 2200,
dimension1: '',
dimension2: '',
dimension3: 'Grey',
Expand Down
31 changes: 19 additions & 12 deletions react/modules/enhancedEcommerceEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
ProductClickData,
ProductViewReferenceId,
} from '../typings/events'
import { AnalyticsEcommerceProduct } from '../typings/gtm'
import { AnalyticsEcommerceProduct, MaybePrice } from '../typings/gtm'

function getSeller(sellers: Seller[]) {
const defaultSeller = sellers.find(seller => seller.sellerDefault)
Expand Down Expand Up @@ -62,6 +62,12 @@ export async function sendEnhancedEcommerceEvents(e: PixelMessage) {
price = undefined
}

const includePrice: MaybePrice = {}

if (typeof price === 'number') {
includePrice.price = price
}

const data = {
ecommerce: {
detail: {
Expand All @@ -77,7 +83,7 @@ export async function sendEnhancedEcommerceEvents(e: PixelMessage) {
dimension2: skuReferenceId ?? '',
dimension3: selectedSku.name,
dimension4: isAvailable,
price,
...includePrice,
},
],
},
Expand Down Expand Up @@ -113,6 +119,12 @@ export async function sendEnhancedEcommerceEvents(e: PixelMessage) {
price = undefined
}

const includePrice: MaybePrice = {}

if (typeof price === 'number') {
includePrice.price = price
}

const data = {
event: 'productClick',
ecommerce: {
Expand All @@ -128,8 +140,9 @@ export async function sendEnhancedEcommerceEvents(e: PixelMessage) {
dimension1: productReference ?? '',
dimension2: sku.referenceId?.Value ?? '',
dimension3: sku.name,
price,

position,
...includePrice,
},
],
},
Expand All @@ -153,10 +166,7 @@ export async function sendEnhancedEcommerceEvents(e: PixelMessage) {
id: item.productId,
variant: item.skuId,
name: item.name, // Product name
price:
item.priceIsInt === true
? `${item.price / 100}`
: `${item.price}`,
price: item.priceIsInt === true ? item.price / 100.0 : item.price,
quantity: item.quantity,
dimension1: item.productRefId ?? '',
dimension2: item.referenceId ?? '', // SKU reference id
Expand Down Expand Up @@ -186,10 +196,7 @@ export async function sendEnhancedEcommerceEvents(e: PixelMessage) {
id: item.productId,
variant: item.skuId,
name: item.name, // Product name
price:
item.priceIsInt === true
? `${item.price / 100}`
: `${item.price}`,
price: item.priceIsInt === true ? item.price / 100.0 : item.price,
quantity: item.quantity,
dimension1: item.productRefId ?? '',
dimension2: item.referenceId ?? '', // SKU reference id
Expand Down Expand Up @@ -371,7 +378,7 @@ function getProductImpressionObjectData(list: string) {
list,
name: product.productName,
position,
price: `${product.sku.seller.commertialOffer.Price}`,
price: product.sku.seller.commertialOffer.Price,
dimension1: product.productReference ?? '',
dimension2: product.sku.referenceId?.Value ?? '',
dimension3: product.sku.name, // SKU name (variation only)
Expand Down
4 changes: 4 additions & 0 deletions react/typings/gtm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ export interface AnalyticsEcommerceProduct {
dimension2: string
dimension3: string
}

export interface MaybePrice {
price?: number
}