Client-Side Counting Standalone Script
If you are using Client-Side Counting for REST API, you may have some pages where the normal Conductrics script tag has not been installed.
Conductrics provides a "standalone" script snippet for such pages.
This snippet is for use with our Client-Side Counting for REST API feature only. It is not needed otherwise.
Because this JavaScript snippet is included (in slightly different form) in the normal Conductrics script tag automatically, you only need to place this snippet on pages that do not have the normal Conductrics script tag.
Installation / Placement
How you "install" this snippet is up to you:
- You may include the snippet "inline", such as:
<script>...snippet here...</script> - Or you may save and include it as a separate asset, for example:
<script src="/js/cc-counts.min.js"></script> - Or you may "bundle" it together with your in-house JavaScript code.
Here is the current version of the script, in both minified and non-minified form:
(function(){if(window.Conductrics==null){window.Conductrics={}}window.Conductrics.ClientCounts={process:function(beacon_url,options){var cc_items,cdl;if(options==null){options={}}if(!(navigator.sendBeacon&&(typeof beacon_url==='string')&&(beacon_url.indexOf('https://')===0))){return}Object.assign(options,{cdl_name:'c_conductrics_data_layer',cleanup:true},options);cdl=window[options.cdl_name];if(Array.isArray(cdl)&&cdl.length){cc_items=cdl.filter(function(item){return item&&(typeof item.cc==='string')});if(cc_items.length){if(options.cleanup){cdl.forEach(function(item){if(cc_items.includes(item)){return cdl.splice(cc_items.indexOf(item),1)}})}return navigator.sendBeacon(beacon_url+'?Action=SendMessage&Version=2012-11-05&MessageBody='+encodeURIComponent(JSON.stringify(cc_items)))}}}}}).call(this);(function() {
if (window.Conductrics == null) {
window.Conductrics = {};
}
window.Conductrics.ClientCounts = {
process: function(beacon_url, options) {
var cc_items, cdl;
if (options == null) {
options = {};
}
if (!(navigator.sendBeacon && (typeof beacon_url === 'string') && (beacon_url.indexOf('https://') === 0))) {
return;
}
Object.assign(options, {
cdl_name: 'c_conductrics_data_layer',
cleanup: true
}, options);
cdl = window[options.cdl_name];
if (Array.isArray(cdl) && cdl.length) {
cc_items = cdl.filter(function(item) {
return item && (typeof item.cc === 'string');
});
if (cc_items.length) {
if (options.cleanup) {
cdl.forEach(function(item) {
if (cc_items.includes(item)) {
return cdl.splice(cc_items.indexOf(item), 1);
}
});
}
return navigator.sendBeacon(beacon_url + '?Action=SendMessage&Version=2012-11-05&MessageBody=' + encodeURIComponent(JSON.stringify(cc_items)));
}
}
}
};
}).call(this);
It's up to you whether to use the minified or non-minified version of the script (you may prefer to use your own bundler/minifier, etc).
Please feel free to contact Conductrics you have any questions about placement.
Calling the process() method
After the snippet is included, a Conductrics.ClientCounts class will be available on the page.
At this time, it provides one method, process(beacon_url), which has one required parameter, beacon_url:
Conductrics.ClientCounts.process("https://example-beacon-url-here");
The
beacon_urlto use is specific to your Conductrics account. Please contact Conductrics for the correct value to use if you don't have it already.
In any case, you want to be sure that the snippet is loaded AFTER your existing <script> tag that includes the contents of ext.cdl from the Conductrics REST API. See Client-Side Counting for REST API and Simple Data Layer for REST API for details.
What Actually Happens?
The actual processing is very simple:
- The snippet looks for the Conductrics "Simple Data Layer" on the page, which is a plain JavaScript array (typically called
window.c_conductrics_data_layer). - The snippet finds any items in the array contain a
ccproperty, which indicates that they should be forwarded to Conductrics for counting (see Client-Side Counting for REST API). - The snippet "sends" the
ccitems to Conductrics via the browser's built-in navigator.sendBeacon API. - The snippet also removes the
ccitems from the "Simple Data Layer" array, with the aim to avoid confusion if there are other systems looking at array's contents. This behavior can be switched off via thecleanupoption (see below) if desired.
Additional Options
The ClientCounts.process() method takes an optional options object as its second argument.
At this time, the available options are:
cdl_name- The name of the Conductrics "Simple Data Layer" that the snippet should look for on the page. The default isc_conductrics_data_layerwhich is the usual name when populating the "Simple Data Layer" with the items returned by our REST API. See Simple Data Layer for REST API for details.cleanup- A boolean flag that controls whether the snippet removes the items it finds for sending. The default istrue. Set tofalseif you prefer theccitems to remain in the "Simple Data Layer" after processing.
For instance, to specify an alternate cdl_name:
Conductrics.ClientCounts.process("https://example-beacon-url-here", {cdl_name: "my_name"});