With minor modifications to the existing smartocto insights tracking script, it is possible to track different conversion events on your pages e.g. user registration, payments, newsletter subscription, and so on.

To send conversion event from a web page where tracking code has been added, call _ain.pushConversion() method with the following syntax:

_ain.pushConversion(<event>, <data>)

Parameters

  • event - string (required)
    Event name to be tracked, example: "Newsletter signup" or "Subscription".
  • data - object (required for the real-time tracking, optional for the insights)
    Object which provides more details associated with the conversion event.
    When used for real-time conversion tracking it must have the 'postid' property which uniquely determines the article and/or the 'url' property with the full article's URL.

Example

<script>
  // Basic tracking script configuration
   ...
  // Conversion tracking
  _ain.pushConversion(
  'Subscription',
      {
        'url': 'https://www.example.com/articles/article_a1b2c3',
        'postid': 'a1b2c3'
      }
  );
</script>

Sometimes, depending on the implementation, the conversion script will not work if the tracking script is not preloaded from the CDN.

The following example is a workaround that ensures the tracker is preloaded before calling the pushConversion() method:

<script type="text/javascript">

  /* BEGIN Insights tracker configuration */
  var _ain = {
    id: "12345",
    trackauto: false
  };
  /* END Insights tracker configuration */

  /* BEGIN load Insights tracker from CDN */
  (function (d, s) {
    var sf = d.createElement(s);
    sf.type = 'text/javascript';
    sf.async = true;
    sf.src = (('https:' == d.location.protocol)
      ? 'https://d7d3cf2e81d293050033-3dfc0615b0fd7b49143049256703bfce.ssl.cf1.rackcdn.com'
      : 'http://t.contentinsights.com') + '/stf.js';
    var t = d.getElementsByTagName(s)[0];
    t.parentNode.insertBefore(sf, t);
  })(document, 'script');
  /* END load Insights tracker from CDN */

  /* BEGIN Conversion tracking */
  (function (a) {
    var _soiStartedAt = Date.now();
    var _soiConversionTrack = function () {
      if (Date.now() - _soiStartedAt > 2000) {
        return;
      }
      if (typeof a.track != 'function') {
        setTimeout(_soiConversionTrack, 100);
        return;
      }
      a.pushConversion(
        'Default conversion',
        {
          'foo': 'fighter',
          'bar': 'tender'
        }
      );
    }
    _soiConversionTrack();
  })(window._ain);
  /* END Conversion tracking */

</script>

Tracking multiple conversions (JavaScript example)

The example below shows how you can track multiple conversion events. Each event is pushed into the queue and is ready to be sent when the tracking script is loaded from the CDN.

<script>
  // BEGIN smartocto insights configuration  
  var _ain = {
    id: "DOMAIN_ID*",
    // trackauto: false
  ...
  };
  // END smartocto insights configuration
  
  // BEGIN conversion event tracking
  var _ainConversionQueue = [];

  var _ainAddConversion = function(event, data) {
    data = data || {};
    if (window._ain && window._ain.LOADED) {
      window._ain.pushConversion(event, data);
    } else {
      _ainConversionQueue.push({event: event, data: data});
    }
  }

  // Flush the queue when the tracker loads
  if (!window._ain.LOADED) {
    _ain.onload = function() {
      for (var i = 0; i < _ainConversionQueue.length; i++) {
        var event = _ainConversionQueue[i]['event'];
        var data  = _ainConversionQueue[i]['data'];
        window._ain.pushConversion(event, data);
      }
    }
  }

  // Event #1
  _ainAddConversion('First conversion', {
    'site': 'domain.com', 
    'type': 'subscription',
    'foo' : 'bar'
  });

  // Event #2
  _ainAddConversion('Second conversion', {});

  // Event #3
  _ainAddConversion('Third conversion');
  // END conversion event tracking

  // BEGIN load smartocto insights tracker
  (function(d, s) {
    var sf = d.createElement(s);
    sf.type = 'text/javascript';
    sf.async = true;
    sf.src = (('https:' == d.location.protocol)
      ? 'https://d7d3cf2e81d293050033-3dfc0615b0fd7b49143049256703bfce.ssl.cf1.rackcdn.com'
      : 'http://t.contentinsights.com') + '/stf.js';
    var t = d.getElementsByTagName(s)[0];
    t.parentNode.insertBefore(sf, t);
  })(document, 'script');
  // END load smartocto insights tracker
</script>

 

IMPORTANT:
Any code that sets _ain.onload (such as the example above) must be executed before loading the smartocto insights tracker.

Please note that function _ainAddConversion(event, data) accepts two parameters:

  1. event (required)
    a string that describes the conversion event, e.g. "annual subscription".
  2. data (required for the real-time tracking, optional for the insights)
    if specified, it must be a valid JSON object.

If you don't want to measure read depth and other metrics on the page where conversion occurs, you can set _ain.trackauto property to false (commented line in the smartocto insights configuration block).

Server-side implementation

Sending conversion events from the server-side can be performed by sending POST requests to the conversion endpoint.

Endpoint:
https://conversions.contentinsights.com/v1/

Parameters:

  • event - string (required)
    Event name to be tracked, for example: "Newsletter signup" or "Subscription".
  • domain_id - string (required)
    The ID of the domain (four-digit number registered in smartocto insights application)
  • data - JSON object (required for the real-time tracking, optional for the insights)
    An object which provides more details associated with the conversion event.
  • ul* - string (optional, required if uid is not specified)
    Unique visitor identifier - value from the _ain_uid first-party cookie.
    Assigned to each visitor upon their first visit to the tracked web page.
  • uid* - string (optional, required if ul is not specified)
    Unique visitor identifier specified by the client.
    It must not be PII (personally identifiable information).
    The value should not be persisted in cookies or other provided storage.
  • timestamp - string (optional)
    Current date time - the number of milliseconds since the Unix Epoch.
    If not specified, the current time will be assigned.

*Using uid as a parameter is advised to be used only when providing ul is not an option.
In case when uid is used as a parameter when issuing conversion tracking request, in order to perform conversion analysis this parameter must also be set on the tracker global _ain object.

 

Example using cURL

curl 'https://conversions.contentinsights.com/v1/' -d '{
  "domain_id": "DOMAIN_ID*",
  "event": "conversion",
  "ul": "1601990772947.378054769.3031728",
  "uid": "0123456789abcdef",
  "data": {
    "label": "Title"
    }
}'

 

Example using PHP

<?php
function postJson($url, $data)
{
    $payload = json_encode($data);
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

$url = 'https://conversions.contentinsights.com/v1/'; 
$uid = 'hash_value_from_client_side'; 
$domain_id = $_GET['domain_id'];
$ul = $_GET[ul];
$now = time();
$custom_data = [
    'value' => 35.0,
    'currency' => 'USD'
];

$data = [
    'domain_id' => $domain_id,
    'ul' => $ul,
    'uid' => $uid,
    'timestamp' => $now,
    'event' => 'conversion',
    'data' => $custom_data
];

print_r(postJson($url, $data));
?>

*DOMAIN_ID is a unique four-digit number assigned when registering the domain for the smartocto insights service.

Please contact smartocto support if you're not sure what value should be used.