Namespace

Granify

Granify

The global Granify object.

Methods

# static setCurrentProduct(selectedProduct)

This function is used to notify Granify which specific product variant is currently selected on a product page.

It should be called immediately after trackProduct, and also whenever the shopper changes the current item in focus on that page.

Parameters:
Name Type Description
selectedProduct object

The lookup information for the currently selected product

sku string

The SKU of the currently selected product. Must match the SKU field in one of the products called in Granify('trackProduct')

product_id string

The parent ID of the product

Tutorials:
Example
Granify('setCurrentProduct', { sku: '123456', product_id: '123' });

# static setDebugLogging(displayLogs)

This function is used to enable or disable displaying Granify logs in console.

You can also enable logs by putting ?granify_debug=true in your page's URL.

Parameters:
Name Type Description
displayLogs boolean

Whether or not to display logs in console

Example
Granify('setDebugLogging', true);

# static setShopperId(shopperId)

This function is used to notify Granify of the current customer's unique identifier. This should be the same identifier you use in your store for your customers. It should not change if the same customer returns.

This method should be called immediately after the page loads.

Parameters:
Name Type Description
shopperId string

The unique identifier for the customer from your internal systems. This should remain the same for the customer on subsequent returns.

Tutorials:
Example
Granify('setShopperId', '123abc');

# static trackCart(cartData)

This function is used to tell Granify what contents the customer currently has in their cart. This implementation requires all cart data, in order to sync the customer's cart with Granify. It's crucial that this is kept up-to-date.

This function should be called:

  • After any change is made to the cart. This includes immediately after 'Buy It Now' or other express purchase buttons, as well as item removal.
  • On each page load (in case a return user comes back to a page with items in their cart)
  • On a new session start (in case a return user comes back to a page with items in their cart)

It is very important that we are in sync with the cart contents. Otherwise, cart-related campaigns will not work.

Parameters:
Name Type Description
cartData object | boolean

The cart data, or false to clear the cart

items Array.<object>

The items contained in the cart

items[].sku string

The specific SKU of the product/variant

items[].product_id string

The parent ID of the product

items[].quantity number

The quantity of this product in the cart

items[].price number

The current unit price of the product, after sales or discounts

items[].regular_price number

The regular price of the product, before sales or discounts

items[].title string

The name or title of the product

items[].image string

The URL of the product image

Example
Granify('trackCart', {
  items: [{
    sku: '123456',
    product_id: '123',
    quantity: 1,
    price: 100.00,
    regular_price: 100.00,
    title: 'My Product Title',
    image: 'https://example.com/example.jpg',
  }, {
    sku: '234567',
    product_id: '234',
    quantity: 1,
    price: 50.00,
    regular_price: 100.00,
    title: 'Another Product Title',
    image: 'https://example.com/example2.jpg',
  }]
});

// To clear the cart:
Granify('trackCart', false);

# static trackOrder(orderData)

This function is used to notify Granify about completed purchases.

It should be called on the page displayed after the order has been finalized & paid (ex. the receipt page). This code needs to be run as soon as possible on the page. The data passed needs to match your analytics.

Parameters:
Name Type Attributes Description
orderData object

An object containing the data for the placed order

currency string

The local currency used for prices in your analytics. Must be "USD", unless another currency has been agreed upon by Granify

order_number string

The unique identifier for this order

total_line_items_price number

The total value of all items in the cart, *before any discounts, taxes, and shipping, but after sale pricing

subtotal_price number

The total value of the items in the order after discounts but before shipping or tax. This should match the order total you use in your own analytics

total_tax number

The total value of any taxes applied to this order

total_shipping number

The total value of any shipping costs applied to this order

total_price number

The total price after discounts, including shipping and possibly taxes

financial_status string

Should always be "paid", unless you want to send information about fraudulent or cancelled orders

line_items Array.<object>

The list of items associated to the order

line_items[].sku string

The specific SKU of the product/variant

line_items[].product_id string

The parent ID of the product

line_items[].price number

The current unit price of the product, after sales or discounts

line_items[].regular_price number

The regular price of the product, before sales or discounts

line_items[].title string

The name or title of the product

discount_codes Array.<object> <optional>

A list of discounts applied to this order.

discount_codes[].code string

The coupon/promo code used

discount_codes[].amount number

The value of the discount

Tutorials:
Example
Granify('trackOrder', {
  currency: 'USD',
  order_number: '1234567890',
  total_line_items_price: 150.00, // (sum of items price)
  subtotal_price: 140.00, // (sum of items price - sum of discount_codes)
  total_tax: 7.00,
  total_shipping: 13.00,
  total_price: 160.00, // (sum of items price - sum of discount_codes + total_tax + total_shipping)
  financial_status: 'paid',
  line_items: [{
    sku: '123456',
    product_id: '123',
    quantity: 1,
    price: 100.00,
    regular_price: 100.00,
    title: 'My Product Title'
  }, {
    sku: '234567',
    product_id: '234',
    quantity: 1,
    price: 50.00,
    regular_price: 100.00,
    title: 'Another Product Title'
  }],
  discount_codes: [{
    code: 'save',
    amount: 10.00
  }]
});

# static trackPageView(pageTypeData)

This function is used to inform Granify about what the current page is.

See Page Types for a list of accepted page types.

Parameters:
Name Type Description
pageTypeData object

An object containing the page type

page_type string

The page type of the current page

Tutorials:
  • Tutorial: track-page-view
Example
Granify('trackPageView', { page_type: 'home' });

# static trackProduct(products)

This function is used to notify Granify about the products on a page. It should be called with an array of product objects with all variations of items on that page. For example, if a shirt had different colors or sizes, the method call should include a product entry for every combination of shirt color and size.

It should be called as soon as possible after trackPageView on a product page.

If only one variant is provided, Granify.setCurrentProduct will be called automatically.

Parameters:
Name Type Attributes Description
products Array.<TrackedProduct>

An array of all product variants on the page

sku string

The specific SKU of the product/variant

product_id string

The parent ID of the product

title string

The name or title of the product

price number

The current unit price of the product, after sales or discounts

regular_price number

The regular price of the product, before sales or discounts

image string

The URL of the product image

in_stock number

The number of items currently in stock

ratings number

The average rating of the product

number_of_reviews number

The number of reviews given for the product

category string

The broad category containing the product

sub_category string <optional>

A secondary category containing the product

color string <optional>

The color of the specific variant

size string <optional>

The size of the specific variant

Tutorials:
Example
// Example with two products/variants on a page
Granify('trackProduct', [{
  sku: '1234567',
  product_id: '1234',
  title: 'Gritty Mask',
  price: 200.00,
  regular_price: 200.00,
  image: 'https://example.com/example.jpg',
  in_stock: 1,
  ratings: 4.5,
  number_of_reviews: 10,
  category: 'Clothing',
  sub_category: 'Masks',
  color: 'orange',
  size: 'large',
}, {
  sku: '56789123',
  product_id: '5678',
  title: 'Gritty Jersey',
  price: 150.00,
  regular_price: 200.00,
  image: 'https://example.com/example2.jpg',
  in_stock: 1,
  ratings: 4.5,
  number_of_reviews: 10,
  category: 'Clothing',
  sub_category: 'Shirts',
  color: 'orange',
  size: 'large',
}]);

# static trackUserData(userData, shouldAppendopt, cacheLimitopt)

This function is used to store any information you want to remember and persist for any user of the device on which it is running. It will persist across pages and multiple sessions.

This method should be called immediately after the page loads, and after the Granify.setShopperId call.

Parameters:
Name Type Attributes Description
userData object

An object containing key-value pairs to be tracked.

shouldAppend boolean <optional>

Whether or not to cache old values into an array with the new value

cacheLimit number <optional>

How many values should be stored in the cache array (oldest values are dropped first when the array is at this size)

Tutorials:
Example
Granify('trackUserData', {
  member_id: '123456',
  rewards_promotion_available: ['reward1', 'reward2'],
  credit_card: true,
  gender: 'm',
  birthday: '12/01'
});

# static trackWishList(wishListData)

This function is used to notify Granify what the current customer's wish lists contain. This method can be called multiple times for different lists you wish to track. Please ensure that you use a unique list_name for each.

This method should be called immediately after the page loads, and after the Granify.setShopperId call.

Parameters:
Name Type Attributes Description
wishListData object

An object containing the data for the wish list

list_name string

The unique identifier for the list. This should remain the same for subsequent calls for the same list

items Array.<object>

The array containing each item in the list

items[].in_stock boolean

A boolean indicating whether the item is currently in stock

items[].product_id string

The parent ID of the product

items[].sku string <optional>

The specific SKU of the product/variant

items[].price number <optional>

The current unit price of the product, after sales or discounts

items[].regular_price number <optional>

The regular price of the product, before sales or discounts

items[].title string <optional>

The name or title of the product

items[].image string <optional>

The URL of the product image

items[].date_added number <optional>

The date when the product was added to the list, in Unix Timestamp Format (ex. 1584274822)

items[].price_when_added number <optional>

The price of the product when it was added to the list

items[].cta_url string <optional>

The URL which links to the product's anchor tag in the list page (ex. https://domain.com/wishlist#1234)

items[].product_url string <optional>

The URL which links to the individual product

Tutorials:
Example
Granify('trackWishList', {
  list_name: 'wishList',
  items: [
    {
      in_stock: true,
      product_id: '1234',
      sku: '123456',
      price: 150.00,
      regular_price: 200.00,
      title: 'Mask',
      image: 'https://example.com/image.jpg',
      date_added: 1584274822,
      price_when_added: 175.00,
      cta_url: 'https://example.com/123456',
      product_url: 'https://example.com/123456',
    }
  ]
});