Conversion Tracking

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.

Quick jump: 

Track conversion: the basics

To send a conversion event from a web page where a 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, for example: "Newsletter signup" or "Subscription".
  • data - object (required)
    An object that provides more details associated with the conversion event.
    It must have the 'postid' property which uniquely determines the article.
    Optionally, it should have the 'url' property with the canonical URL of the article (without any additional parameters, e.g. UTM tags and so on).

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 Insights tracking script (stf.js) is not preloaded from the CDN.

Please make sure that _ain.id is of the "string" type.

You can perform a simple check if you open the page where the Insights script should be triggered and execute this one-line code in the Console window:

window._ain.pushConversion('conversion-test',{'postid': '0123456789abcdef'});

If the Insights script is properly configured, you should see this output:

Conversion successfully sent.

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 */
window._ain = {
id: "1234",
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',
'postid': 'a1b2c3'
}
);
}
_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
window._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', {
'url': 'domain.com/article/a1b2c3',
'postid' : 'a1b2c3'
});

// 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": {
"postid": "a1b2c3"
}
}'

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()*1000;
$custom_data = [
  'url' => window.location.href,
  'postid' => 'a1b2c3'
];

$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. You can get it from any article on the domain where the Insights script is implemented, by typing _ain.id in the Console.

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