Skip to main content

init

A JSON object containing a snapshot of the page at time of page render. It will always have the present Context of the page, as well as the Data field, which provides access to the Cart and Customer objects.

Anchor to context
context
Anchor to customerPrivacy
customerPrivacy
Examples
import {register} from '@shopify/web-pixels-extension';

register(({analytics, init}) => {
analytics.subscribe('page_viewed', (event) => {
// On every page view, get the current state of the cart

const customer = init.data.customer;
const cart = init.data.cart;
const shop = init.data.shop;
const purchasingCompany = init.data.purchasingCompany;

console.log(`Customer Name: ${customer.firstName}`);
// Customer Name: Bogus

console.log(`Total Number of Items in Cart: ${cart.totalQuantity}`);
// Total Number of Items in Cart: 3

console.log(`Total Cost of Cart: ${cart.cost.totalAmount.amount}`);
// Total Cost of Cart: 50.82

console.log(`Shop name: ${shop.name}`);
// Shop name: Shop 123

console.log(`Shop currency code: ${shop.paymentSettings.currencyCode}`);
// Shop currency code: CAD

console.log(`Purchasing company name: ${purchasingCompany.company.name}`);
// Purchasing company name: Acme Corporation

console.log(
`Purchasing company location name: ${purchasingCompany.location.name}`,
);
// Purchasing company location name: Toronto fulfillment center
});
});
Was this page helpful?