@chordcommerce/analytics
Version:
Chord Commerce event tracking
541 lines (540 loc) • 39.5 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { addCdpSnippet, cdpQueue } from './cdp.js';
import { eventSchemas } from '../validators/index.js';
import { pruneNullValues } from '../utils.js';
export class ChordAnalytics {
constructor(options) {
var _a, _b;
/**
* Allows snippet.js to detect whether this library has been initialized yet.
*/
this.initialize = true;
/**
* Allows snippet.js to detect whether the snippet has started running yet.
*/
this.invoked = true;
/*
* Return the CDP instance or the queue.
*/
this.cdp = () => {
const { cdp, namespace } = this.options;
const a = cdp || (window === null || window === void 0 ? void 0 : window[`_${namespace}`]) || cdpQueue(namespace);
if (typeof a === 'function')
return a();
return a;
};
/*
* Log a message to the console if `options.enableLogging` is true.
*/
this.logger = (message, ...optionalParams) => {
if (!this.options.enableLogging)
return;
// eslint-disable-next-line no-console
console.log(message, ...optionalParams);
};
/**
* Validate the body of a `track` event against the Chord tracking plan.
*/
this.validate = (event, props) => {
if (!event) {
this.logger('No event name provided');
return [{ success: false }];
}
const schema = event && eventSchemas[event];
if (!schema)
return [{ success: true, data: props }];
return schema.map((s) => s.safeParse(props));
};
/**
* Generate the event `meta` property.
*/
this.meta = () => {
var _a, _b, _c, _d, _e, _f;
return Object.assign(Object.assign({}, this.options.metadata), { ownership: {
oms_id: (_b = (_a = this.options.metadata) === null || _a === void 0 ? void 0 : _a.ownership) === null || _b === void 0 ? void 0 : _b.omsId,
store_id: (_d = (_c = this.options.metadata) === null || _c === void 0 ? void 0 : _c.ownership) === null || _d === void 0 ? void 0 : _d.storeId,
tenant_id: (_f = (_e = this.options.metadata) === null || _e === void 0 ? void 0 : _e.ownership) === null || _f === void 0 ? void 0 : _f.tenantId,
}, version: {
// TODO: make this dynamic
major: 3,
minor: 0,
patch: 0,
} });
};
/**
* Send a `track` event to the CDP with any event name and properties.
*/
this.track = (event, props, options) => {
if (!event)
this.logger('No event name provided');
const formattedProps = this.options.stripNull && props ? pruneNullValues(props) : props;
const finalEventProps = Object.assign(Object.assign({}, formattedProps), { meta: this.meta() });
if (this.options.debug) {
this.validate(event, finalEventProps).forEach((valid) => {
if (!valid.success) {
this.logger('Chord tracking plan violation', valid.error);
}
});
}
return new Promise((resolve) => {
try {
const a = this.cdp();
if (a && typeof a.track === 'function') {
a.track(event, finalEventProps, options, () => resolve());
}
}
catch (error) {
this.logger(error);
}
});
};
/**
* Send an `identify` event to the CDP with user id and traits.
*/
this.identify = (...props) => {
try {
const a = this.cdp();
if (a && typeof a.identify === 'function') {
a.identify(...props);
}
}
catch (error) {
this.logger(error);
}
};
/**
* Send a `page` event to the CDP.
*/
this.page = () => {
try {
const a = this.cdp();
if (a && typeof a.page === 'function') {
a.page({ meta: this.meta() });
}
}
catch (error) {
this.logger(error);
}
};
this.reset = () => {
try {
const a = this.cdp();
if (a && typeof a.reset === 'function') {
a.reset();
}
}
catch (error) {
this.logger(error);
}
};
// TODO: Add support for props.products
this.trackCartViewed = (props, options) => __awaiter(this, void 0, void 0, function* () {
var _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
const cart = (_f = (_e = (_d = (_c = this.options) === null || _c === void 0 ? void 0 : _c.formatters) === null || _d === void 0 ? void 0 : _d.objects) === null || _e === void 0 ? void 0 : _e.cart) === null || _f === void 0 ? void 0 : _f.call(_e, {
cart: props === null || props === void 0 ? void 0 : props.cart,
});
const payload = {
cart_id: cart === null || cart === void 0 ? void 0 : cart.cart_id,
currency: cart === null || cart === void 0 ? void 0 : cart.currency,
products: cart === null || cart === void 0 ? void 0 : cart.products,
value: cart === null || cart === void 0 ? void 0 : cart.value,
};
if ((_j = (_h = (_g = this.options) === null || _g === void 0 ? void 0 : _g.formatters) === null || _h === void 0 ? void 0 : _h.events) === null || _j === void 0 ? void 0 : _j.cartViewed) {
const formattedPayload = (_m = (_l = (_k = this.options) === null || _k === void 0 ? void 0 : _k.formatters) === null || _l === void 0 ? void 0 : _l.events) === null || _m === void 0 ? void 0 : _m.cartViewed(props, payload);
return this.track('Cart Viewed', formattedPayload, options);
}
return this.track('Cart Viewed', payload, options);
});
// TODO: Add support for props.products
this.trackCheckoutStarted = (props, options) => __awaiter(this, void 0, void 0, function* () {
var _o, _p, _q, _r, _s, _t, _u;
const formatter = (_q = (_p = (_o = this.options) === null || _o === void 0 ? void 0 : _o.formatters) === null || _p === void 0 ? void 0 : _p.events) === null || _q === void 0 ? void 0 : _q.checkoutStarted;
const checkout = (_u = (_t = (_s = (_r = this.options) === null || _r === void 0 ? void 0 : _r.formatters) === null || _s === void 0 ? void 0 : _s.objects) === null || _t === void 0 ? void 0 : _t.checkout) === null || _u === void 0 ? void 0 : _u.call(_t, {
checkout: props === null || props === void 0 ? void 0 : props.checkout,
});
const payload = checkout;
if (typeof formatter === 'function') {
const formattedPayload = formatter(props, payload);
return this.track('Checkout Started', formattedPayload, options);
}
return this.track('Checkout Started', payload, options);
});
this.trackCheckoutStepCompleted = (props, options) => __awaiter(this, void 0, void 0, function* () {
var _v, _w, _x;
const formatter = (_x = (_w = (_v = this.options) === null || _v === void 0 ? void 0 : _v.formatters) === null || _w === void 0 ? void 0 : _w.events) === null || _x === void 0 ? void 0 : _x.checkoutStepCompleted;
const payload = {
checkout_id: props === null || props === void 0 ? void 0 : props.checkoutId,
payment_method: props === null || props === void 0 ? void 0 : props.paymentMethod,
shipping_method: props === null || props === void 0 ? void 0 : props.shippingMethod,
step: props === null || props === void 0 ? void 0 : props.step,
};
if (typeof formatter === 'function') {
const formattedPayload = formatter(props, payload);
return this.track('Checkout Step Completed', formattedPayload, options);
}
return this.track('Checkout Step Completed', payload, options);
});
this.trackCheckoutStepViewed = (props, options) => __awaiter(this, void 0, void 0, function* () {
var _y, _z, _0;
const formatter = (_0 = (_z = (_y = this.options) === null || _y === void 0 ? void 0 : _y.formatters) === null || _z === void 0 ? void 0 : _z.events) === null || _0 === void 0 ? void 0 : _0.checkoutStepViewed;
const payload = {
checkout_id: props === null || props === void 0 ? void 0 : props.checkoutId,
payment_method: props === null || props === void 0 ? void 0 : props.paymentMethod,
shipping_method: props === null || props === void 0 ? void 0 : props.shippingMethod,
step: props === null || props === void 0 ? void 0 : props.step,
};
if (typeof formatter === 'function') {
const formattedPayload = formatter(props, payload);
return this.track('Checkout Step Viewed', formattedPayload, options);
}
return this.track('Checkout Step Viewed', payload, options);
});
this.trackCouponApplied = (props, options) => __awaiter(this, void 0, void 0, function* () {
var _1, _2, _3;
const formatter = (_3 = (_2 = (_1 = this.options) === null || _1 === void 0 ? void 0 : _1.formatters) === null || _2 === void 0 ? void 0 : _2.events) === null || _3 === void 0 ? void 0 : _3.couponApplied;
const payload = {
cart_id: props === null || props === void 0 ? void 0 : props.cartId,
coupon_id: props === null || props === void 0 ? void 0 : props.couponId,
coupon_name: props === null || props === void 0 ? void 0 : props.couponName,
discount: props === null || props === void 0 ? void 0 : props.discount,
order_id: props === null || props === void 0 ? void 0 : props.orderId,
};
if (typeof formatter === 'function') {
const formattedPayload = formatter(props, payload);
return this.track('Coupon Applied', formattedPayload, options);
}
return this.track('Coupon Applied', payload, options);
});
this.trackCouponDenied = (props, options) => __awaiter(this, void 0, void 0, function* () {
var _4, _5, _6;
const formatter = (_6 = (_5 = (_4 = this.options) === null || _4 === void 0 ? void 0 : _4.formatters) === null || _5 === void 0 ? void 0 : _5.events) === null || _6 === void 0 ? void 0 : _6.couponDenied;
const payload = {
cart_id: props === null || props === void 0 ? void 0 : props.cartId,
coupon_id: props === null || props === void 0 ? void 0 : props.couponId,
coupon_name: props === null || props === void 0 ? void 0 : props.couponName,
order_id: props === null || props === void 0 ? void 0 : props.orderId,
reason: props === null || props === void 0 ? void 0 : props.reason,
};
if (typeof formatter === 'function') {
const formattedPayload = formatter(props, payload);
return this.track('Coupon Denied', formattedPayload, options);
}
return this.track('Coupon Denied', payload, options);
});
this.trackCouponEntered = (props, options) => __awaiter(this, void 0, void 0, function* () {
var _7, _8, _9;
const formatter = (_9 = (_8 = (_7 = this.options) === null || _7 === void 0 ? void 0 : _7.formatters) === null || _8 === void 0 ? void 0 : _8.events) === null || _9 === void 0 ? void 0 : _9.couponEntered;
const payload = {
cart_id: props === null || props === void 0 ? void 0 : props.cartId,
coupon_id: props === null || props === void 0 ? void 0 : props.couponId,
coupon_name: props === null || props === void 0 ? void 0 : props.couponName,
order_id: props === null || props === void 0 ? void 0 : props.orderId,
};
if (typeof formatter === 'function') {
const formattedPayload = formatter(props, payload);
return this.track('Coupon Entered', formattedPayload, options);
}
return this.track('Coupon Entered', payload, options);
});
this.trackCouponRemoved = (props, options) => __awaiter(this, void 0, void 0, function* () {
var _10, _11, _12;
const formatter = (_12 = (_11 = (_10 = this.options) === null || _10 === void 0 ? void 0 : _10.formatters) === null || _11 === void 0 ? void 0 : _11.events) === null || _12 === void 0 ? void 0 : _12.couponRemoved;
const payload = {
cart_id: props === null || props === void 0 ? void 0 : props.cartId,
coupon_id: props === null || props === void 0 ? void 0 : props.couponId,
coupon_name: props === null || props === void 0 ? void 0 : props.couponName,
discount: props === null || props === void 0 ? void 0 : props.discount,
order_id: props === null || props === void 0 ? void 0 : props.orderId,
};
if (typeof formatter === 'function') {
const formattedPayload = formatter(props, payload);
return this.track('Coupon Removed', formattedPayload, options);
}
return this.track('Coupon Removed', payload, options);
});
this.trackEmailCaptured = (props, options) => __awaiter(this, void 0, void 0, function* () {
var _13, _14, _15;
const formatter = (_15 = (_14 = (_13 = this.options) === null || _13 === void 0 ? void 0 : _13.formatters) === null || _14 === void 0 ? void 0 : _14.events) === null || _15 === void 0 ? void 0 : _15.emailCaptured;
const payload = {
email: props === null || props === void 0 ? void 0 : props.email,
placement_component: props === null || props === void 0 ? void 0 : props.placementComponent,
placement_page: props === null || props === void 0 ? void 0 : props.placementPage,
};
if (typeof formatter === 'function') {
const formattedPayload = formatter(props, payload);
return this.track('Email Captured', formattedPayload, options);
}
return this.track('Email Captured', payload, options);
});
this.trackProductAdded = (props, options) => __awaiter(this, void 0, void 0, function* () {
var _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27;
const formatter = (_18 = (_17 = (_16 = this.options) === null || _16 === void 0 ? void 0 : _16.formatters) === null || _17 === void 0 ? void 0 : _17.events) === null || _18 === void 0 ? void 0 : _18.productAdded;
const product = (_22 = (_21 = (_20 = (_19 = this.options) === null || _19 === void 0 ? void 0 : _19.formatters) === null || _20 === void 0 ? void 0 : _20.objects) === null || _21 === void 0 ? void 0 : _21.product) === null || _22 === void 0 ? void 0 : _22.call(_21, props === null || props === void 0 ? void 0 : props.product);
const cart = (_26 = (_25 = (_24 = (_23 = this.options) === null || _23 === void 0 ? void 0 : _23.formatters) === null || _24 === void 0 ? void 0 : _24.objects) === null || _25 === void 0 ? void 0 : _25.cart) === null || _26 === void 0 ? void 0 : _26.call(_25, {
cart: props === null || props === void 0 ? void 0 : props.cart,
});
const quantity = ((_27 = props === null || props === void 0 ? void 0 : props.product) === null || _27 === void 0 ? void 0 : _27.quantity) || 1;
const payload = Object.assign(Object.assign({}, product), { cart_id: cart.cart_id, currency: cart.currency, products: [Object.assign(Object.assign({}, product), { quantity })], total: product.price * (quantity || 1), value: product.price * (quantity || 1) });
if (typeof formatter === 'function') {
const formattedPayload = formatter(props, payload);
return this.track('Product Added', formattedPayload, options);
}
return this.track('Product Added', payload, options);
});
this.trackProductClicked = (props, options) => __awaiter(this, void 0, void 0, function* () {
var _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40;
const formatter = (_30 = (_29 = (_28 = this.options) === null || _28 === void 0 ? void 0 : _28.formatters) === null || _29 === void 0 ? void 0 : _29.events) === null || _30 === void 0 ? void 0 : _30.productClicked;
const product = (_34 = (_33 = (_32 = (_31 = this.options) === null || _31 === void 0 ? void 0 : _31.formatters) === null || _32 === void 0 ? void 0 : _32.objects) === null || _33 === void 0 ? void 0 : _33.product) === null || _34 === void 0 ? void 0 : _34.call(_33, props === null || props === void 0 ? void 0 : props.product);
const cart = (_38 = (_37 = (_36 = (_35 = this.options) === null || _35 === void 0 ? void 0 : _35.formatters) === null || _36 === void 0 ? void 0 : _36.objects) === null || _37 === void 0 ? void 0 : _37.cart) === null || _38 === void 0 ? void 0 : _38.call(_37, {
cart: props === null || props === void 0 ? void 0 : props.cart,
});
const quantity = ((_39 = props === null || props === void 0 ? void 0 : props.product) === null || _39 === void 0 ? void 0 : _39.quantity) || 1;
const payload = Object.assign(Object.assign({}, product), { cart_id: cart.cart_id, currency: cart.currency, item_list_id: (_40 = props === null || props === void 0 ? void 0 : props.listId) === null || _40 === void 0 ? void 0 : _40.toString(), item_list_name: props === null || props === void 0 ? void 0 : props.listName, products: [Object.assign(Object.assign({}, product), { quantity })] });
if (typeof formatter === 'function') {
const formattedPayload = formatter(props, payload);
return this.track('Product Clicked', formattedPayload, options);
}
return this.track('Product Clicked', payload, options);
});
this.trackVariantClicked = (props, options) => __awaiter(this, void 0, void 0, function* () {
var _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52;
const formatter = (_43 = (_42 = (_41 = this.options) === null || _41 === void 0 ? void 0 : _41.formatters) === null || _42 === void 0 ? void 0 : _42.events) === null || _43 === void 0 ? void 0 : _43.variantClicked;
const product = (_47 = (_46 = (_45 = (_44 = this.options) === null || _44 === void 0 ? void 0 : _44.formatters) === null || _45 === void 0 ? void 0 : _45.objects) === null || _46 === void 0 ? void 0 : _46.product) === null || _47 === void 0 ? void 0 : _47.call(_46, props === null || props === void 0 ? void 0 : props.product);
const cart = (_51 = (_50 = (_49 = (_48 = this.options) === null || _48 === void 0 ? void 0 : _48.formatters) === null || _49 === void 0 ? void 0 : _49.objects) === null || _50 === void 0 ? void 0 : _50.cart) === null || _51 === void 0 ? void 0 : _51.call(_50, {
cart: props === null || props === void 0 ? void 0 : props.cart,
});
const quantity = ((_52 = props === null || props === void 0 ? void 0 : props.product) === null || _52 === void 0 ? void 0 : _52.quantity) || 1;
const payload = Object.assign(Object.assign({}, product), { cart_id: cart.cart_id, currency: cart.currency, quantity: quantity, line_item_id: props === null || props === void 0 ? void 0 : props.lineItemId, coupon: props === null || props === void 0 ? void 0 : props.coupon });
if (typeof formatter === 'function') {
const formattedPayload = formatter(props, payload);
return this.track('Variant Clicked', formattedPayload, options);
}
return this.track('Variant Clicked', payload, options);
});
this.trackProductListFiltered = (props, options) => __awaiter(this, void 0, void 0, function* () {
var _53, _54, _55, _56, _57;
const formatter = (_55 = (_54 = (_53 = this.options) === null || _53 === void 0 ? void 0 : _53.formatters) === null || _54 === void 0 ? void 0 : _54.events) === null || _55 === void 0 ? void 0 : _55.productListFiltered;
const payload = {
category: props === null || props === void 0 ? void 0 : props.category,
filters: props === null || props === void 0 ? void 0 : props.filters,
list_id: (_56 = props === null || props === void 0 ? void 0 : props.listId) === null || _56 === void 0 ? void 0 : _56.toString(),
item_list_id: (_57 = props === null || props === void 0 ? void 0 : props.listId) === null || _57 === void 0 ? void 0 : _57.toString(),
item_list_name: props === null || props === void 0 ? void 0 : props.listName,
sorts: props === null || props === void 0 ? void 0 : props.sorts,
};
if (typeof formatter === 'function') {
const formattedPayload = formatter(props, payload);
return this.track('Product List Filtered', formattedPayload, options);
}
return this.track('Product List Filtered', payload, options);
});
this.trackProductListViewed = (props, options) => __awaiter(this, void 0, void 0, function* () {
var _58, _59, _60, _61, _62, _63;
const formatter = (_60 = (_59 = (_58 = this.options) === null || _58 === void 0 ? void 0 : _58.formatters) === null || _59 === void 0 ? void 0 : _59.events) === null || _60 === void 0 ? void 0 : _60.productListViewed;
const payload = {
category: props === null || props === void 0 ? void 0 : props.category,
list_id: (_61 = props === null || props === void 0 ? void 0 : props.listId) === null || _61 === void 0 ? void 0 : _61.toString(),
item_list_id: (_62 = props === null || props === void 0 ? void 0 : props.listId) === null || _62 === void 0 ? void 0 : _62.toString(),
item_list_name: props === null || props === void 0 ? void 0 : props.listName,
products: (_63 = props === null || props === void 0 ? void 0 : props.products) === null || _63 === void 0 ? void 0 : _63.map((p, i) => {
var _a, _b, _c, _d;
return (_d = (_c = (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.formatters) === null || _b === void 0 ? void 0 : _b.objects) === null || _c === void 0 ? void 0 : _c.product) === null || _d === void 0 ? void 0 : _d.call(_c, Object.assign(Object.assign({}, p), { position: i + 1 }));
}),
value: 0, // TODO: remove from tracking plan
};
if (typeof formatter === 'function') {
const formattedPayload = formatter(props, payload);
return this.track('Product List Viewed', formattedPayload, options);
}
return this.track('Product List Viewed', payload, options);
});
this.trackProductRemoved = (props, options) => __awaiter(this, void 0, void 0, function* () {
var _64, _65, _66, _67, _68, _69, _70, _71, _72, _73, _74, _75, _76, _77, _78;
const formatter = (_66 = (_65 = (_64 = this.options) === null || _64 === void 0 ? void 0 : _64.formatters) === null || _65 === void 0 ? void 0 : _65.events) === null || _66 === void 0 ? void 0 : _66.productRemoved;
/*
const product = this.options?.formatters?.objects?.product?.({
position: props?.position,
product: props?.product,
quantity: props?.quantity,
variantId: props?.variantId,
})
*/
const lineItem = (_70 = (_69 = (_68 = (_67 = this.options) === null || _67 === void 0 ? void 0 : _67.formatters) === null || _68 === void 0 ? void 0 : _68.objects) === null || _69 === void 0 ? void 0 : _69.lineItem) === null || _70 === void 0 ? void 0 : _70.call(_69, {
lineItem: props === null || props === void 0 ? void 0 : props.lineItem,
});
const cart = (_74 = (_73 = (_72 = (_71 = this.options) === null || _71 === void 0 ? void 0 : _71.formatters) === null || _72 === void 0 ? void 0 : _72.objects) === null || _73 === void 0 ? void 0 : _73.cart) === null || _74 === void 0 ? void 0 : _74.call(_73, {
cart: props === null || props === void 0 ? void 0 : props.cart,
});
const payload = Object.assign(Object.assign({}, lineItem), { cart_id: cart.cart_id, currency: cart.currency, products: [lineItem], total: ((_75 = lineItem === null || lineItem === void 0 ? void 0 : lineItem.price) !== null && _75 !== void 0 ? _75 : 0) * ((_76 = lineItem === null || lineItem === void 0 ? void 0 : lineItem.quantity) !== null && _76 !== void 0 ? _76 : 1), value: ((_77 = lineItem === null || lineItem === void 0 ? void 0 : lineItem.price) !== null && _77 !== void 0 ? _77 : 0) * ((_78 = lineItem === null || lineItem === void 0 ? void 0 : lineItem.quantity) !== null && _78 !== void 0 ? _78 : 1) });
if (typeof formatter === 'function') {
const formattedPayload = formatter(props, payload);
return this.track('Product Removed', formattedPayload, options);
}
return this.track('Product Removed', payload, options);
});
this.trackProductViewed = (props, options) => __awaiter(this, void 0, void 0, function* () {
var _79, _80, _81, _82, _83, _84, _85, _86, _87, _88, _89, _90, _91;
const formatter = (_81 = (_80 = (_79 = this.options) === null || _79 === void 0 ? void 0 : _79.formatters) === null || _80 === void 0 ? void 0 : _80.events) === null || _81 === void 0 ? void 0 : _81.productViewed;
const product = (_85 = (_84 = (_83 = (_82 = this.options) === null || _82 === void 0 ? void 0 : _82.formatters) === null || _83 === void 0 ? void 0 : _83.objects) === null || _84 === void 0 ? void 0 : _84.product) === null || _85 === void 0 ? void 0 : _85.call(_84, props === null || props === void 0 ? void 0 : props.product);
const cart = (_89 = (_88 = (_87 = (_86 = this.options) === null || _86 === void 0 ? void 0 : _86.formatters) === null || _87 === void 0 ? void 0 : _87.objects) === null || _88 === void 0 ? void 0 : _88.cart) === null || _89 === void 0 ? void 0 : _89.call(_88, {
cart: props === null || props === void 0 ? void 0 : props.cart,
});
const quantity = ((_90 = props === null || props === void 0 ? void 0 : props.product) === null || _90 === void 0 ? void 0 : _90.quantity) || 1;
const payload = Object.assign(Object.assign({}, product), { cart_id: cart.cart_id, currency: cart.currency, products: [Object.assign(Object.assign({}, product), { quantity })], value: product.price * (((_91 = props === null || props === void 0 ? void 0 : props.product) === null || _91 === void 0 ? void 0 : _91.quantity) || 1) });
if (typeof formatter === 'function') {
const formattedPayload = formatter(props, payload);
return this.track('Product Viewed', formattedPayload, options);
}
return this.track('Product Viewed', payload, options);
});
this.trackProductsSearched = (props, options) => __awaiter(this, void 0, void 0, function* () {
var _92, _93, _94, _95;
const formatter = (_94 = (_93 = (_92 = this.options) === null || _92 === void 0 ? void 0 : _92.formatters) === null || _93 === void 0 ? void 0 : _93.events) === null || _94 === void 0 ? void 0 : _94.productsSearched;
const payload = {
currency: props === null || props === void 0 ? void 0 : props.currency,
price: typeof (props === null || props === void 0 ? void 0 : props.price) !== 'undefined' ? Number(props === null || props === void 0 ? void 0 : props.price) : undefined,
product_id: (_95 = props === null || props === void 0 ? void 0 : props.productId) === null || _95 === void 0 ? void 0 : _95.toString(),
quantity: props === null || props === void 0 ? void 0 : props.quantity,
query: props === null || props === void 0 ? void 0 : props.query,
};
if (typeof formatter === 'function') {
const formattedPayload = formatter(props, payload);
return this.track('Products Searched', formattedPayload, options);
}
return this.track('Products Searched', payload, options);
});
this.trackSignedIn = (props, options) => __awaiter(this, void 0, void 0, function* () {
var _96, _97, _98;
const formatter = (_98 = (_97 = (_96 = this.options) === null || _96 === void 0 ? void 0 : _96.formatters) === null || _97 === void 0 ? void 0 : _97.events) === null || _98 === void 0 ? void 0 : _98.signedIn;
const payload = {
email: props === null || props === void 0 ? void 0 : props.email,
method: props === null || props === void 0 ? void 0 : props.method,
};
if (typeof formatter === 'function') {
const formattedPayload = formatter(props, payload);
return this.track('Signed In', formattedPayload, options);
}
return this.track('Signed In', payload, options);
});
this.trackSignedOut = (props, options) => __awaiter(this, void 0, void 0, function* () {
var _99, _100, _101;
const formatter = (_101 = (_100 = (_99 = this.options) === null || _99 === void 0 ? void 0 : _99.formatters) === null || _100 === void 0 ? void 0 : _100.events) === null || _101 === void 0 ? void 0 : _101.signedOut;
const payload = {
email: props === null || props === void 0 ? void 0 : props.email,
};
if (typeof formatter === 'function') {
const formattedPayload = formatter(props || {}, payload);
return this.track('Signed Out', formattedPayload, options);
}
return this.track('Signed Out', payload, options);
});
this.trackSignedUp = (props, options) => __awaiter(this, void 0, void 0, function* () {
var _102, _103, _104;
const formatter = (_104 = (_103 = (_102 = this.options) === null || _102 === void 0 ? void 0 : _102.formatters) === null || _103 === void 0 ? void 0 : _103.events) === null || _104 === void 0 ? void 0 : _104.signedUp;
const payload = {
email: props === null || props === void 0 ? void 0 : props.email,
method: props === null || props === void 0 ? void 0 : props.method,
};
if (typeof formatter === 'function') {
const formattedPayload = formatter(props || {}, payload);
return this.track('Signed Up', formattedPayload, options);
}
return this.track('Signed Up', payload, options);
});
this.trackLoginStarted = (props, options) => __awaiter(this, void 0, void 0, function* () {
var _105, _106, _107;
const formatter = (_107 = (_106 = (_105 = this.options) === null || _105 === void 0 ? void 0 : _105.formatters) === null || _106 === void 0 ? void 0 : _106.events) === null || _107 === void 0 ? void 0 : _107.loginStarted;
const payload = {
email: props === null || props === void 0 ? void 0 : props.email,
};
if (typeof formatter === 'function') {
const formattedPayload = formatter(props, payload);
return this.track('Login Started', formattedPayload, options);
}
return this.track('Login Started', payload, options);
});
this.trackSubscriptionCancelled = (props, options) => __awaiter(this, void 0, void 0, function* () {
var _108, _109, _110, _111;
const formatter = (_110 = (_109 = (_108 = this.options) === null || _108 === void 0 ? void 0 : _108.formatters) === null || _109 === void 0 ? void 0 : _109.events) === null || _110 === void 0 ? void 0 : _110.subscriptionCancelled;
const payload = {
actionable_date: props === null || props === void 0 ? void 0 : props.actionableDate,
address: props === null || props === void 0 ? void 0 : props.address,
brand: props === null || props === void 0 ? void 0 : props.brand,
email: props === null || props === void 0 ? void 0 : props.email,
end_date: props === null || props === void 0 ? void 0 : props.endDate,
interval_length: props === null || props === void 0 ? void 0 : props.intervalLength,
interval_units: props === null || props === void 0 ? void 0 : props.intervalUnits,
products: (_111 = props === null || props === void 0 ? void 0 : props.products) === null || _111 === void 0 ? void 0 : _111.map((p, i) => {
var _a, _b, _c, _d;
return (_d = (_c = (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.formatters) === null || _b === void 0 ? void 0 : _b.objects) === null || _c === void 0 ? void 0 : _c.product) === null || _d === void 0 ? void 0 : _d.call(_c, Object.assign(Object.assign({}, p), { position: i + 1 }));
}),
state: props === null || props === void 0 ? void 0 : props.state,
subscription_id: props === null || props === void 0 ? void 0 : props.subscriptionId,
currency: props === null || props === void 0 ? void 0 : props.currency,
};
if (typeof formatter === 'function') {
const formattedPayload = formatter(props || {}, payload);
return this.track('Subscription Cancelled', formattedPayload, options);
}
return this.track('Subscription Cancelled', payload, options);
});
this.trackNavigationClicked = (props, options) => __awaiter(this, void 0, void 0, function* () {
var _112, _113, _114;
const formatter = (_114 = (_113 = (_112 = this.options) === null || _112 === void 0 ? void 0 : _112.formatters) === null || _113 === void 0 ? void 0 : _113.events) === null || _114 === void 0 ? void 0 : _114.navigationClicked;
const payload = {
category: props === null || props === void 0 ? void 0 : props.category,
label: props === null || props === void 0 ? void 0 : props.label,
navigation_placement: props === null || props === void 0 ? void 0 : props.navigationPlacement,
navigation_title: props === null || props === void 0 ? void 0 : props.navigationTitle,
navigation_url: props === null || props === void 0 ? void 0 : props.navigationUrl,
};
if (typeof formatter === 'function') {
const formattedPayload = formatter(props, payload);
return this.track('Navigation Clicked', formattedPayload, options);
}
return this.track('Navigation Clicked', payload, options);
});
this.trackPaymentInfoEntered = (props, options) => __awaiter(this, void 0, void 0, function* () {
var _115, _116, _117, _118;
const formatter = (_117 = (_116 = (_115 = this.options) === null || _115 === void 0 ? void 0 : _115.formatters) === null || _116 === void 0 ? void 0 : _116.events) === null || _117 === void 0 ? void 0 : _117.paymentInfoEntered;
const payload = {
checkout_id: props === null || props === void 0 ? void 0 : props.checkoutId,
coupon: props === null || props === void 0 ? void 0 : props.coupon,
currency: props === null || props === void 0 ? void 0 : props.currency,
order_id: props === null || props === void 0 ? void 0 : props.orderId,
payment_method: props === null || props === void 0 ? void 0 : props.paymentMethod,
products: (_118 = props === null || props === void 0 ? void 0 : props.products) === null || _118 === void 0 ? void 0 : _118.map((p, i) => {
var _a, _b, _c, _d;
return (_d = (_c = (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.formatters) === null || _b === void 0 ? void 0 : _b.objects) === null || _c === void 0 ? void 0 : _c.product) === null || _d === void 0 ? void 0 : _d.call(_c, Object.assign(Object.assign({}, p), { position: i + 1 }));
}),
shipping_method: props === null || props === void 0 ? void 0 : props.shippingMethod,
step: props === null || props === void 0 ? void 0 : props.step,
value: props === null || props === void 0 ? void 0 : props.value,
};
if (typeof formatter === 'function') {
const formattedPayload = formatter(props, payload);
return this.track('Payment Info Entered', formattedPayload, options);
}
return this.track('Payment Info Entered', payload, options);
});
const defaults = {
debug: false,
enableLogging: true,
namespace: 'chord',
stripNull: true,
};
this.options = Object.assign(Object.assign({}, defaults), options);
if (((_a = this.options) === null || _a === void 0 ? void 0 : _a.cdpDomain) && ((_b = this.options) === null || _b === void 0 ? void 0 : _b.cdpWriteKey) && window) {
window[this.options.namespace] = (window === null || window === void 0 ? void 0 : window[this.options.namespace]) || this;
addCdpSnippet(this.options);
}
}
}