@shopana/ga
Version:
Type-safe Google Analytics 4 (GA4) tracking library for React and Next.js with ecommerce support, event batching, and SSR compatibility
89 lines • 4.2 kB
JavaScript
import {} from '../core/AnalyticsClient';
import { buildAddToCartEvent, buildPurchaseEvent, buildRemoveFromCartEvent, buildViewCartEvent, buildBeginCheckoutEvent, buildAddPaymentInfoEvent, buildAddShippingInfoEvent, buildViewItemEvent, buildViewItemListEvent, buildSelectItemEvent, buildViewPromotionEvent, buildSelectPromotionEvent, buildAddToWishlistEvent, buildGenerateLeadEvent, } from '../domain/commerceTracking';
import {} from '../types/events';
import { TransactionRegistry } from '../domain/TransactionRegistry';
import { getMeasurementId } from './utils';
export class CommerceTracker {
constructor(client, onDuplicateWarning = console.warn) {
this.client = client;
this.onDuplicateWarning = onDuplicateWarning;
this.transactionRegistry = new TransactionRegistry();
}
purchase(params) {
const measurementId = getMeasurementId(params, this.client);
const fullParams = {
...params,
measurementId,
};
return this.client.track(buildPurchaseEvent(fullParams, this.transactionRegistry, this.onDuplicateWarning));
}
addToCart(params) {
const measurementId = getMeasurementId(params, this.client);
const fullParams = { ...params, measurementId };
return this.client.track(buildAddToCartEvent(fullParams));
}
removeFromCart(params) {
const measurementId = getMeasurementId(params, this.client);
const fullParams = { ...params, measurementId };
return this.client.track(buildRemoveFromCartEvent(fullParams));
}
viewCart(params) {
const measurementId = getMeasurementId(params, this.client);
const fullParams = { ...params, measurementId };
return this.client.track(buildViewCartEvent(fullParams));
}
beginCheckout(params) {
const measurementId = getMeasurementId(params, this.client);
const fullParams = { ...params, measurementId };
return this.client.track(buildBeginCheckoutEvent(fullParams));
}
addPaymentInfo(params) {
const measurementId = getMeasurementId(params, this.client);
const fullParams = { ...params, measurementId };
return this.client.track(buildAddPaymentInfoEvent(fullParams));
}
addShippingInfo(params) {
const measurementId = getMeasurementId(params, this.client);
const fullParams = { ...params, measurementId };
return this.client.track(buildAddShippingInfoEvent(fullParams));
}
viewItem(params) {
const measurementId = getMeasurementId(params, this.client);
const fullParams = { ...params, measurementId };
return this.client.track(buildViewItemEvent(fullParams));
}
viewItemList(params) {
const measurementId = getMeasurementId(params, this.client);
const fullParams = { ...params, measurementId };
return this.client.track(buildViewItemListEvent(fullParams));
}
selectItem(params) {
const measurementId = getMeasurementId(params, this.client);
const fullParams = { ...params, measurementId };
return this.client.track(buildSelectItemEvent(fullParams));
}
viewPromotion(params) {
const measurementId = getMeasurementId(params, this.client);
const fullParams = { ...params, measurementId };
return this.client.track(buildViewPromotionEvent(fullParams));
}
selectPromotion(params) {
const measurementId = getMeasurementId(params, this.client);
const fullParams = { ...params, measurementId };
return this.client.track(buildSelectPromotionEvent(fullParams));
}
addToWishlist(params) {
const measurementId = getMeasurementId(params, this.client);
const fullParams = { ...params, measurementId };
return this.client.track(buildAddToWishlistEvent(fullParams));
}
generateLead(params) {
const measurementId = getMeasurementId(params, this.client);
const fullParams = { ...params, measurementId };
return this.client.track(buildGenerateLeadEvent(fullParams));
}
destroy() {
this.transactionRegistry.clear();
}
}
//# sourceMappingURL=CommerceTracker.js.map