Javascript API

You can interact with the LoyaltyLion SDK using its JavaScript API. You can use this to get information about the program, such as rules and rewards, and information about the customer if someone is logged in.

Overview

The LoyaltyLion SDK adds itself to the window.lion variable. If something else is already using lion, it’ll be available instead as window.loyaltylion. Our documentation references window.lion, but the two variables can be used interchangeably.

Initialization

lion.init is called automatically for Shopify, BigCommerce and Magento installations.

lion.init should be called once per page load to initialize the LoyaltyLion SDK and (if applicable) authenticate the current logged in customer. You’ll need your LoyaltyLion site token to identify your store.

lion.init({
  token: 'YOUR_SITE_TOKEN',
})

If a customer is logged in, their information and authentication data should be passed in, using a server-side generated auth token.

lion.init({
  token: 'YOUR_SITE_TOKEN',
  customer: {
    id: '1001', // unique customer ID
    email: 'alice@example.com', // customer email address
  },
  auth: {
    date: '2018-01-01T10:00:00Z', // date in ISO8601 format
    token: 'AUTH_TOKEN', // token, generated server-side
  },
})

Authentication

The LoyaltyLion SDK automatically authenticates the current customer for standard Shopify, BigCommerce and Magento installations.

The best way to authenticate the customer is by passing the customer and auth data to the lion.init call. This ensures the customer is authenticated as soon as the LoyaltyLion SDK is ready.

If you need to authenticate the customer later, e.g. if they’re able to log in without a full page reload, or authentication happens after the page load, you can use the lion.authenticateCustomer method.

This method accepts the same customer and auth properties as lion.init. It needs a server-side generated auth token.

lion.authenticateCustomer({
  customer: {
    id: '1001', // unique customer ID
    email: 'alice@example.com', // customer email address
  },
  auth: {
    date: '2018-01-01T10:00:00Z', // date in ISO8601 format
    token: 'AUTH_TOKEN', // token, generated server-side
  },
})