Skip to content

Single page apps ​

User Tracking ​

Tracking user behavior on single page apps can be done in a similar way to the general way of user tracking.

You simple have to load the Verada tracking script:

https://cdn.fanplayr.com/client/production/loader.js

And then wait for Verada to load by supplying a function "fanplayr_ready":

javascript
window.fanplayr_ready = function (fanplayr, platform) {
  // ...
};

Before directly calling the tracking as follows:

javascript
window.fanplayr.reinitialize(DATA);

An example is below. You can use this on your site. The tracking calls are there simply as an example of how it would work. The first two calls would wait for Verada to load, and the third would be called right away (given that Verada is likely to have loaded within 2 seconds).

html
<script>
  // You can use this code on your site to cache calls to Verada before it has loaded.
  // Note that "window.fanplayr_ready" should be defined before loading the Verada JS
  (function () {
    var trackingCache = [];
    var isReady = false;

    // this is called when the Verada platform is ready
    window.fanplayr_ready = function (fanplayr, platform) {
      isReady = true;
      var i;
      // track any cached tracking calls
      for (i = 0; i < trackingCache.length; i++) {
        fanplayr.reinitialize(trackingCache[i]);
      }
      trackingCache = [];
    };

    window.fanplayr_track = function (data) {
      if (isReady) {
        // track
        window.fanplayr.reinitialize(data);
      } else {
        // cache this tracking call
        trackingCache.push(data);
      }
    };
  })();
</script>

<script>
  // This script block is here to demo how tracking would work.
  // Note the data itself is valid for the call but will not track anything.
  window.fanplayr_track({
    data: {}
  });
  window.fanplayr_track({
    data: {}
  });
  setTimeout(function () {
    window.fanplayr_track({
      data: {}
    });
  }, 2000);
</script>

<!-- This is the script that you need to include AFTER setting the "window.fanplayr_ready" function -->
<script src="https://cdn.fanplayr.com/client/production/loader.js"></script>

The data is of the following format, and is the same as is set in normal page tracking.

javascript
{
    version: 3,
    accountKey: '',
    applyToCartUrl: '',
    sessionOfferUrl: '',
    data: {
      lineItemCount: 0,
      numItems: 0,
      subTotal: 0.00,
      total: 0.00,
      discount: 0.00,
      discountCode: '',
      pageType: '',
      categoryId: '',
      categoryName: '',
      productId: '',
      productName: '',
      productSku: '',
      productPrice: 0.00,
      productUrl: '',
      productImage: '',
      currency: '',
      products : [
        {
          id: '',
          sku: '',
          price: 0.00,
          qty: 1,
          name: ''
        }
      ],
      cartAction: 'override'
    },
    custom_data: {
      // ...
    }
  }

Order Tracking ​

Order tracking relies on the same script to be loaded as in user tracking. In a single page app it is likely that this will already be loaded.

To set data for order tracking you need only to set the window.fp_sales_order variable, including the same data as in general order tracking.

javascript
window.fp_sales_orders = {
  version: 3,
  accountKey: '',
  storeDomain: '',
  data: {
    orderId: '',
    orderNumber: '',
    subTotal: 0.0,
    total: 0.0,
    discount: 0.0,
    discountCode: '',
    shipping: 0.0,
    tax: 0.0,
    currency: '',
    orderEmail: '',
    firstName: '',
    lastName: '',
    customerEmail: '',
    customerId: '',
    products: [
      {
        id: '',
        sku: '',
        price: 0.0,
        qty: 1,
        name: ''
      }
    ],
    cartAction: 'override'
  }
};

You can then track the order by calling:

javascript
// track the order using details in fp_sales_order
window.fanplayr.platform.trackOrder();
// clears the current state, in case we make another order in the same session
window.fanplayr.platform.state.clear();