Skip to content

Commit

Permalink
Add options in the standard API and default to expect currency in one…
Browse files Browse the repository at this point in the history
… of the possible ways
  • Loading branch information
igneel64 committed Apr 21, 2023
1 parent 1877ba8 commit d70353b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 71 deletions.
60 changes: 38 additions & 22 deletions plugins/browser-plugin-snowplow-ecommerce/src/ga4/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import {
import { Currency, GA4EcommerceObject, ItemList, Transaction, Promotion } from './types';
import { transformG4ItemsToSPProducts, transformGA4PromotionToSPPromotion } from './utils';

type Options = {
currency?: string;
};

const DEFAULT_PAYMENT_METHOD = 'unknown';

export function trackGA4ViewCart() {
Expand All @@ -28,55 +32,67 @@ export function trackGA4SelectPromotion(ecommerce: GA4EcommerceObject & Promotio
trackPromotionClick(promotion);
}

export function trackGA4AddToCart(ecommerce: GA4EcommerceObject & Currency, finalCartValue: number) {
const products = transformG4ItemsToSPProducts(ecommerce);
trackAddToCart({ products, total_value: finalCartValue, currency: ecommerce.currency });
export function trackGA4AddToCart(
ecommerce: GA4EcommerceObject & Currency,
opts: Options & { finalCartValue: number }
) {
const currency = (ecommerce.currency || opts.currency)!;
const products = transformG4ItemsToSPProducts(ecommerce, currency);
trackAddToCart({ products, total_value: opts.finalCartValue, currency });
}

export function trackGA4RemoveFromCart(ecommerce: GA4EcommerceObject & Currency, finalCartValue: number) {
const products = transformG4ItemsToSPProducts(ecommerce);
trackRemoveFromCart({ products, total_value: finalCartValue, currency: ecommerce.currency });
export function trackGA4RemoveFromCart(
ecommerce: GA4EcommerceObject & Currency,
opts: Options & { finalCartValue: number }
) {
const currency = (ecommerce.currency || opts.currency)!;
const products = transformG4ItemsToSPProducts(ecommerce, currency);
trackRemoveFromCart({ products, total_value: opts.finalCartValue, currency });
}

export function trackGA4ViewItem(ecommerce: GA4EcommerceObject) {
const products = transformG4ItemsToSPProducts(ecommerce);
export function trackGA4ViewItem(ecommerce: GA4EcommerceObject, opts: Options) {
const currency = (ecommerce.currency || opts.currency)!;
const products = transformG4ItemsToSPProducts(ecommerce, currency);
trackProductView({ ...products[0] });
}

export function trackGA4ViewItemList(ecommerce: GA4EcommerceObject & ItemList) {
const products = transformG4ItemsToSPProducts(ecommerce);
export function trackGA4ViewItemList(ecommerce: GA4EcommerceObject & ItemList, opts: Options) {
const currency = (ecommerce.currency || opts.currency)!;
const products = transformG4ItemsToSPProducts(ecommerce, currency);
trackProductListView({ name: ecommerce.item_list_id || ecommerce.item_list_name, products });
}

export function trackGA4SelectItem(ecommerce: GA4EcommerceObject & ItemList) {
const [product] = transformG4ItemsToSPProducts(ecommerce);
export function trackGA4SelectItem(ecommerce: GA4EcommerceObject & ItemList, opts: Options) {
const currency = (ecommerce.currency || opts.currency)!;
const [product] = transformG4ItemsToSPProducts(ecommerce, currency);
trackProductListClick({ name: ecommerce.item_list_id || ecommerce.item_list_name, product });
}

export function trackGA4BeginCheckout(step: number = 1) {
trackCheckoutStep({ step });
export function trackGA4BeginCheckout(opts: { step?: number }) {
trackCheckoutStep({ step: opts.step || 1 });
}

export function trackGA4AddShippingInfo(ecommerce: GA4EcommerceObject, step: number) {
trackCheckoutStep({ step, delivery_method: ecommerce.shipping_tier });
export function trackGA4AddShippingInfo(ecommerce: GA4EcommerceObject, opts: { step: number }) {
trackCheckoutStep({ step: opts.step, delivery_method: ecommerce.shipping_tier });
}

export function trackGA4AddPaymentOptions(ecommerce: GA4EcommerceObject, step: number) {
trackCheckoutStep({ step, payment_method: ecommerce.payment_type });
export function trackGA4AddPaymentOptions(ecommerce: GA4EcommerceObject, opts: { step: number }) {
trackCheckoutStep({ step: opts.step, payment_method: ecommerce.payment_type });
}

export function trackGA4Transaction(
ecommerce: GA4EcommerceObject & Transaction,
paymentMethod = DEFAULT_PAYMENT_METHOD
opts: Options & { paymentMethod: string }
) {
const products = transformG4ItemsToSPProducts(ecommerce);
const currency = (ecommerce.currency || opts.currency)!;
const products = transformG4ItemsToSPProducts(ecommerce, currency);

trackTransaction({
products,
transaction_id: ecommerce.transaction_id,
currency: ecommerce.currency,
currency,
revenue: ecommerce.value,
payment_method: paymentMethod,
payment_method: opts.paymentMethod || DEFAULT_PAYMENT_METHOD,
tax: ecommerce.tax,
shipping: ecommerce.shipping,
});
Expand Down
9 changes: 4 additions & 5 deletions plugins/browser-plugin-snowplow-ecommerce/src/ga4/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ interface GA4ItemTransformation {
categorySeparator?: string;
}

export function transformG4ItemsToSPProducts({
items,
currency = 'USD',
categorySeparator = '/',
}: GA4ItemTransformation): Product[] {
export function transformG4ItemsToSPProducts(
{ items, categorySeparator = '/' }: GA4ItemTransformation,
currency: string
): Product[] {
return items.map((ga4Item) => {
const { item_category, item_category2, item_category3, item_category4, item_category5 } = ga4Item;
const category = [item_category, item_category2, item_category3, item_category4, item_category5]
Expand Down
69 changes: 35 additions & 34 deletions plugins/browser-plugin-snowplow-ecommerce/src/ua/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import {

import { transformUAProductsToSPProducts, transformUAPromotionsToSPPromotions } from './utils';

type Options = {
currency?: string;
};

const DEFAULT_PAYMENT_METHOD = 'unknown';

export function trackEnhancedEcommercePromoView(ecommerce: UAEnhancedEcommerceObject & PromoViewEvent) {
Expand All @@ -42,47 +46,46 @@ export function trackEnhancedEcommercePromoClick(ecommerce: UAEnhancedEcommerceO

export function trackEnhancedEcommerceAddToCart(
ecommerce: UAEnhancedEcommerceObject & AddToCartEvent,
finalCartValue: number
opts: Options & { finalCartValue: number }
) {
const products = transformUAProductsToSPProducts({
currencyCode: ecommerce.currencyCode,
products: ecommerce.add.products,
});
trackAddToCart({ products, total_value: finalCartValue, currency: ecommerce.currencyCode });
const currency = (ecommerce.currencyCode || opts.currency)!;
const products = transformUAProductsToSPProducts(ecommerce.add.products, currency);
trackAddToCart({ products, total_value: opts.finalCartValue, currency });
}

export function trackEnhancedEcommerceRemoveFromCart(
ecommerce: UAEnhancedEcommerceObject & RemoveFromCartEvent,
finalCartValue: number
opts: Options & { finalCartValue: number }
) {
const products = transformUAProductsToSPProducts({
currencyCode: ecommerce.currencyCode,
products: ecommerce.remove.products,
});
trackRemoveFromCart({ products, total_value: finalCartValue, currency: ecommerce.currencyCode });
const currency = (ecommerce.currencyCode || opts.currency)!;
const products = transformUAProductsToSPProducts(ecommerce.remove.products, currency);
trackRemoveFromCart({ products, total_value: opts.finalCartValue, currency });
}

export function trackEnhancedEcommerceProductDetail(ecommerce: UAEnhancedEcommerceObject & ProductDetailEvent) {
const products = transformUAProductsToSPProducts({
currencyCode: ecommerce.currencyCode,
products: ecommerce.detail.products,
});
export function trackEnhancedEcommerceProductDetail(
ecommerce: UAEnhancedEcommerceObject & ProductDetailEvent,
opts: Options
) {
const currency = (ecommerce.currencyCode || opts.currency)!;
const products = transformUAProductsToSPProducts(ecommerce.detail.products, currency);
trackProductView({ ...products[0] });
}

export function trackEnhancedEcommerceProductListView(ecommerce: UAEnhancedEcommerceObject & ProductListViewEvent) {
const products = transformUAProductsToSPProducts({
currencyCode: ecommerce.currencyCode,
products: ecommerce.impressions,
});
export function trackEnhancedEcommerceProductListView(
ecommerce: UAEnhancedEcommerceObject & ProductListViewEvent,
opts: Options
) {
const currency = (ecommerce.currencyCode || opts.currency)!;
const products = transformUAProductsToSPProducts(ecommerce.impressions, currency);
trackProductListView({ name: ecommerce.impressions[0].list!, products });
}

export function trackEnhancedEcommerceProductListClick(ecommerce: UAEnhancedEcommerceObject & ProductListClickEvent) {
const [product] = transformUAProductsToSPProducts({
currencyCode: ecommerce.currencyCode,
products: ecommerce.click.products,
});
export function trackEnhancedEcommerceProductListClick(
ecommerce: UAEnhancedEcommerceObject & ProductListClickEvent,
opts: Options
) {
const currency = (ecommerce.currencyCode || opts.currency)!;
const [product] = transformUAProductsToSPProducts(ecommerce.click.products, currency);
trackProductListClick({ name: ecommerce.click.actionField.list, product });
}

Expand All @@ -95,18 +98,16 @@ export function trackEnhancedEcommerceCheckoutStep(

export function trackEnhancedEcommercePurchase(
ecommerce: UAEnhancedEcommerceObject & PurchaseEvent,
paymentMethod = DEFAULT_PAYMENT_METHOD
opts: Options & { paymentMethod: string }
) {
const products = transformUAProductsToSPProducts({
products: ecommerce.purchase.products,
currencyCode: ecommerce.currencyCode,
});
const currency = (ecommerce.currencyCode || opts.currency)!;
const products = transformUAProductsToSPProducts(ecommerce.purchase.products, currency);
trackTransaction({
products,
transaction_id: ecommerce.purchase.actionField.id,
currency: ecommerce.currencyCode,
currency,
revenue: ecommerce.purchase.actionField.revenue,
payment_method: paymentMethod,
payment_method: opts.paymentMethod || DEFAULT_PAYMENT_METHOD,
tax: ecommerce.purchase.actionField.tax,
shipping: ecommerce.purchase.actionField.shipping,
});
Expand Down
12 changes: 2 additions & 10 deletions plugins/browser-plugin-snowplow-ecommerce/src/ua/utils.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import { Product, Promotion } from '../types';
import { EEProduct, EEPromo } from './types';

interface UAProductsTransformation {
products: EEProduct[];
currencyCode?: string;
}

export function transformUAProductsToSPProducts({
products,
currencyCode = 'USD',
}: UAProductsTransformation): Product[] {
export function transformUAProductsToSPProducts(products: EEProduct[], currency: string): Product[] {
return products.map((product) => {
return {
currency: currencyCode,
currency,
/* Id or name is required, but we in SP-plugin we require id to be present */
id: product.id! || product.name!,
price: product.price!,
Expand Down

0 comments on commit d70353b

Please sign in to comment.