Source: entrypoint.js

import { currentWindowState, sendTrackingBody, setPurchaseItems } from "./core";
import { TrackingHost, sessionIDKey } from "../../common/src/core/config";
import { currentCampaignId } from "../../common/src/core/campaign";
import {
  getExperienceConfigFromStorage,
  readCookie,
  getTestExclusionsFromStorage,
} from "../../common/src/user/experiences";

// OMITTED
// @param {Object} [params.customer]
// @param {boolean} [params.customer.doNotStorePersonalization=false] - Set to true if you do not want purchased item ids to be stored in the customer's browser localStorage for Stylitics personalization purposes.

/**
 * Sends purchase tracking data to Stylitics to help us deliver reports on key metrics
 * @module styliticsTrackPurchase
 *
 * @param {Object} params
 * @param {string} params.client - The username given to you by Stylitics
 * @param {string} params.orderId - A unique ID for this order. This is useful for reconciling Stylitics' metrics with client metrics per order
 * @param {number} params.orderTotal - the total cost of the customer's order without shipping, tax or individual promo-code discounts
 * @param {Object[]} params.items - The items that make up this order. If more than one of the same item is purchased in the same order, enter that item object N times for N quantity of the item purchased.
 * @param {string} params.items[].id - The id for each item in the order. These should
 * correspond to the same type of identifier you pass to the Stylitics Widget, typically a colorway-level ID (should be the same identifiers you give as "item_number" in the widget config)
 * @param {number} params.items[].price - the cost of each item in the order.
 * @param {string} [params.currency="USD"] - If this purchase was completed using a currency other than US Dollars pass an ISO 4217 Alphabetic code for the currency used. (e.g. "CAD")
 * @param {string} [params.locale] - See `locale` in the widget's class documentation; use the same locale here.
 * @param {"production" | "staging"} [params.environment="production"] - Set this to "staging" only in your staging environments in order to send purchase events to the Stylitics staging tracking environment.


 * @exports module:styliticsTrackPurchase.
 *
 * @example
 * Can be used via a module:
 *
 * <script type="module">
 *   import styliticsTrackPurchase from "https://web-assets.stylitics.com/purchase-tracking/latest/purchase_tracking.js";
 *   styliticsTrackPurchase({
 *      // in your staging environment please uncomment:
 *      // environment: "staging",
 *      client: "your-stylitics-username",
 *      orderId: "Ord-123",
 *      orderTotal: 95.25,
 *      items: [
 *      {
 *        id: "item-id-234",
 *        price: 35.00
 *      },
 *      {
 *        id: "item-id-342",
 *        price: 60.25
 *      }]})
 * </script>
 * @example
 * Can also be used via a script:
 *
 * <script src="https://web-assets.stylitics.com/purchase-tracking/latest/purchase_tracking_iife.js"></script>
 * <script>
 * StyliticsPixel({
 *
 *      // in your staging environment please uncomment:
 *      // environment: "staging",
 *      client: "your-stylitics-username",
 *      orderId: "Ord-123",
 *      orderTotal: 95.25,
 *      items: [
 *      {
 *        id: "item-id-234",
 *        price: 35.00
 *      },
 *      {
 *        id: "item-id-342",
 *        price: 60.25
 *      }]})
 * </script>
 */
export default function send({ client, orderId, orderTotal, items, environment, customer, currency, locale }) {
  // read from cookie for backwards compability with V2
  const sessionID =
    localStorage.getItem(sessionIDKey) || readCookie(document.cookie, "styliticsWidgetSession") || "ineligible";

  const body = {
    client,
    order_id: orderId,
    order_total: orderTotal,
    items,
    currency: currency || "USD",
    // attempting to account for "unset" values better than a default of en-US
    // because we have some clients who are say UK-only
    locale: locale || "unset",
    session_id: sessionID,
    campaign_id: currentCampaignId(),
    // TODO remove these
    fingerprint_id: sessionID,
    ab_group: "ineligible",
    ...currentWindowState(),
  };

  const experienceConfig = getExperienceConfigFromStorage();
  if (experienceConfig) {
    body.experience_config = experienceConfig;
  }

  const testExclusions = getTestExclusionsFromStorage();
  if (testExclusions) {
    body.ab_exclusions = testExclusions;
  }

  const domain = environment === "staging" ? "https://datastream-staging.stylitics.com" : TrackingHost;
  const url = `${domain}/api/purchases`;

  sendTrackingBody(url, body);

  if (customer?.doNotStorePersonalization !== true) {
    setPurchaseItems(
      client,
      items.map((i) => i.id)
    );
  }
}