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

Fix AddToCart currency values #16

Merged
merged 1 commit into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- Currency values in AddToCart event.

## [2.2.0] - 2020-10-20

Expand Down
18 changes: 14 additions & 4 deletions react/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getProductPrice } from './utils/formatHelper'
import { ProductOrder, PixelMessage } from './typings/events'
import { ProductOrder, PixelMessage, Product } from './typings/events'
import { canUseDOM } from 'vtex.render-runtime'

function handleMessages(e: PixelMessage) {
Expand Down Expand Up @@ -41,12 +40,12 @@ function handleMessages(e: PixelMessage) {
const { items, currency } = e.data

fbq('track', 'AddToCart', {
value: items.reduce((acc, item) => acc + item.price, 0),
value: items.reduce((acc, item) => acc + item.price, 0) / 100,
content_ids: items.map(sku => sku.skuId),
contents: items.map(sku => ({
id: sku.skuId,
quantity: sku.quantity,
item_price: sku.price,
item_price: sku.price / 100,
})),
content_type: 'product',
currency: currency,
Expand All @@ -58,6 +57,17 @@ function handleMessages(e: PixelMessage) {
}
}

function getProductPrice(product: Product) {
let price
try {
price = product.items[0].sellers[0].commertialOffer.Price
} catch {
price = undefined
}
return price
Comment on lines +61 to +67
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let price
try {
price = product.items[0].sellers[0].commertialOffer.Price
} catch {
price = undefined
}
return price
return product?.items?.[0]?.sellers?.[0]?.commertialOffer?.Price

}


if (canUseDOM) {
window.addEventListener('message', handleMessages)
}
2 changes: 1 addition & 1 deletion react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
"tslint": "^5.11.0",
"tslint-config-vtex": "^2.0.0",
"tslint-eslint-rules": "^5.4.0",
"typescript": "3.8.3"
"typescript": "3.9.7"
}
}
11 changes: 0 additions & 11 deletions react/utils/formatHelper.ts

This file was deleted.

8 changes: 4 additions & 4 deletions react/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,10 @@ tsutils@^3.0.0:
dependencies:
tslib "^1.8.1"

typescript@3.8.3:
version "3.8.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.8.3.tgz#409eb8544ea0335711205869ec458ab109ee1061"
integrity sha512-MYlEfn5VrLNsgudQTVJeNaQFUAI7DkhnOjdpAp4T+ku1TfQClewlbSuTVHiA+8skNBgaf02TL/kLOvig4y3G8w==
typescript@3.9.7:
version "3.9.7"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.9.7.tgz#98d600a5ebdc38f40cb277522f12dc800e9e25fa"
integrity sha512-BLbiRkiBzAwsjut4x/dsibSTB6yWpwT5qWmC2OfuCg3GgVQCSgMs4vEctYPhsaGtd0AeuuHMkjZ2h2WG8MSzRw==

wrappy@1:
version "1.0.2"
Expand Down