trackPurchase()

Prev Next

trackPurchase() is used to track when a user makes a purchase on a website. This data will be used for analytics and for powering XGen’s AI predictions.

id Must match exactly 1 to 1 with a prod_code in the XGen catalog. If these do not match exactly, product tracking and predictions will not function properly.

You can check to ensure they are the same by going to the XGen platform and going to the “Catalogs” section. Select the catalog you are tracking and search for the id you are passing into the event.

Syntax

trackPurchase(orderDetails);

Parameters

orderDetails

type: object
description: Contains information on the purchase order.
keys:

orderId

type: string
description: The unique ID associated with this order.

total

type: number or string
default value: The sum of the price of the products array.
description: The total cost of this order. Note: if this value is excluded, total will be calculated by the sum of all of the products included in the order.

tax (OPTIONAL)

type: number or string
description: The tax amount associated with this order.

discount (OPTIONAL)

type: number or string
description: The amount taken off of the original order total for sales or discount codes.

currency

type: string (currency code)
description: The currency code that will be used when purchasing the PDP’s product. Make sure it is a three letter currency code in the format of 'USD', 'EUR', 'JPY', etc.

products

type: array
description: The products that were purchased in this order.
items:

productObject

type: object
description: A product that was purchased in this order.
keys:

id

type: string
description: The unique prod_code for the PDP page’s product. Make sure this id matches exactly 1 to 1 with a prod_code in XGen’s catalog. (see warning at the top for more info)

name

type: string
description: The name of the PDP page’s product.

price

type: number or string
description: The price of the PDP page’s product. Make sure this matches the price that the user will pay for that product. If the product is on sale, the marked down price should be used.

quantity

type: number
default value: 1
description: The number of this product that was purchase.

Return Value

This function has no return value.

Examples

Track a purchase event

In this example, we are grabbing the order info from a global variable. We are then mapping the product info so it matches the correct format. Then the trackPurchase() function is called to send the event to XGen’s API.

// Import the integration from the 'installation' step
import { integration } from './xgenSdkIntegration';

// Get order info
const order = window.orderInfo;

// Map product info from order info
const productInfo = order.products.map(product => {
  id: product.sku,
  name: product.name,
  price: product.price,
  quantity: product.count // defaults to 1
});

// Send purchase event
integration.trackPurchase({
  orderId: order.transactionId,
  total: order.orderValue, // default value is a sum of the products
  tax: order.tax, // optional
  discount: order.discount, // optional
  currency: 'USD',
  products: productInfo
});