HomeDev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

Ajax tracking only

Describes how to configure tracking for when the element or form is loaded or refreshed on a webpage using Ajax.

Ajax tracking is necessary when the element or form is loaded, or refreshed on a webpage using Ajax, and needs to send the update to Optimizely Recommendations tracking system. This keeps reporting and tracking accurate and correctly optimizes site recommendations and tracking based on data gathered.

Supported page types

The following page types are supported:

Page typeExample Ajax call
basketPeerius.sendAjax("/basket/add.pagex?basket=" + basket);
checkoutPeerius.sendAjax("/checkout/add.pagex?checkout=" + checkout);
wishlistPeerius.sendAjax("/wishlist/add.pagex?wishlist=" + wishlist);

Scenarios

Scenario 1: Product added to basket

When a new product is added to the basket, and is updated in the basket using Ajax.

Current basket has 2 items: product 1 and product 2 with quantity 2 and 1 and unit prices of 30.50 GBP and 15.00 GBP respectively.

ProductQuantityPrice
product 1230.50
product 2115.00

And the user adds one more item, product 3, to the basket with quantity 1 and unit price 10.00 GBP. So the basket will look like this:

ProductQuantityPrice
product 1230.50
product 2115.00
product 3110.00

Scenario 2: Quantity of a product is altered

When the quantity of a product in the basket is altered, and is updated in the basket using Ajax.

Current basket has 2 items: product 1 and product 2 with quantity 2 and 1 and unit prices of 30.50 GBP and

15.00 GBP respectively.

ProductQuantityPrice
product 1230.50
product 2115.00

And the user reduces the quantity of the item product 1 to 1. So the basket will now look like this:

ProductQuantityPrice
product 1130.50
product 2115.00

Scenario 3: Product removed from basket

When a product is removed from the basket, and is updated in the basket using Ajax.

Current basket has 2 items: product 1 and product 2 with quantity 2 and 1 and unit prices of 30.50 GBP and 15.00 GBP respectively.

ProductQuantityPrice
product 1230.50
product 2115.00

And the user removes the item product 1 from the basket. So the basket will now look like this:

ProductQuantityPrice
product 2115.00

Implementation

The implementation of Ajax tracking consists of three stages:

  • Stage 1 – An updated version of the changes needs to be stored in a variable.
  • Stage 2 – The contents of the variable need to be encoded by calling the encodeURIComponent method.
  • Stage 3 – Send the updates to the Optimizely Recommendations tracking system.
var basket = {
                   "items"    : [{
                                   "refCode" : "product1",
                                   "qty"     : 2,
                                   "price"   : 30.50
                                 }, 
                                 {
                                   "refCode" : "product2", 
                                   "qty"     : 1,
                                   "price"   : 15.00
                                }],
                   "currency" : "GBP"
     }

Stage 1: Save the changes

An updated version of the basket changes needs to be stored in a variable defined as a JSON object.

Example:  Updated variable basket for Scenario 1.

var basket = { 
                   "items"    : [{
                                   "refCode" : "product1", 
                                   "qty"     : 2,
                                   "price"   : 30.50
                                 }, 
                                 {
                                   "refCode" : "product2", 
                                   "qty"     : 1,
                                   "price"   : 15.00
                                 }, 
                                 {
                                   "refCode" : "product3", 
                                   "qty"     : 1,
                                   "price"   : 10.00
                                }],
                   "currency" : "GBP"
                 }

Example:  Updated variable basket for Scenario 2.

var basket = { 
                   "items"    : [{
                                   "refCode" : "product1", 
                                   "qty"     : 1,
                                   "price"   : 30.50
                                 }, 
                                 {
                                   "refCode" : "product2", 
                                   "qty"     : 1,
                                   "price"   : 15.00
                                }],
                   "currency" : "GBP"
                 }

Example:  Updated variable basket for Scenario 3.

var basket = { 
                   "items"    : [{
                                   "refCode" : "product2", 
                                   "qty"     : 1,
                                   "price"   : 15.00
                                }],
                   "currency" : "GBP"
                 }

Stage 2: Encode the changes

After you have created the variable basket, which holds the update of the basket, the next stage is to encode the contents of the variable using the encodeURIComponent method.

basket = encodeURIComponent(JSON.stringify(basket));

Stage 3: Send changes to Optimizely

The next and final stage is to send the basket updates to the Optimizely Recommendations tracking system.

Peerius.sendAjax("<clientname>/basket/add.pagex?basket=" + basket);

📘

Note

The element is unique for your site and will be provided by Optimizely.

Additional parameters

You can add the following parameters to the URL to send Optimizely additional information.

ParameterExample Ajax call
locationPeerius.sendAjax("/basket/add.pagex?basket="+basket+"&location="+location)
userPeerius.sendAjax("/basket/add.pagex?basket="+basket+"&user="+user)

Example: Variable location

var location = "en"

Example: Variable user

var user = { "id" : "abcd1234efgh" }
    user = encodeURIComponent(JSON.stringify(user));

If sending both these variables, the Ajax call looks like this:

Peerius.sendAjax("<clientname>/basket/add.pagex?basket="+basket+"&location="+location+"&user="+user)