autochart-tracker
Version:
JavaScript API for tracking visitor activities on automotive websites with Autochart.io
45 lines (39 loc) • 1.94 kB
JavaScript
// Snippet to asynchronously load the autochart.track.js library.
// Tracking methods are stubbed and calls to them are saved to be replayed later
window.autochart = window.autochart || [];
window.autochart.methods = [
'init', 'page', 'trackVehicleView', 'trackSearch', 'trackVisitIntent', 'tag', 'trackLead', 'trackLeadForm', 'trackVehicleAction', 'trackFinance', 'ready', 'trackLeadFormAspNet'
];
// Define a factory to create queue stubs. These are placeholders for the
// "real" methods in analytics.js so that you never have to wait for the library
// to load asynchronously to actually track things. The `method` is always the
// first argument, so we know which method to replay the call into.
window.autochart.factory = function (method) {
return function () {
var args = Array.prototype.slice.call(arguments);
args.unshift(method);
window.autochart.push(args);
return window.autochart;
};
};
// For each of our methods, generate a queueing method.
for (var i = 0; i < window.autochart.methods.length; i++) {
var method = window.autochart.methods[i];
window.autochart[method] = window.autochart.factory(method);
}
// Define a method that will asynchronously load autochart.track.js
window.autochart.load = function (apiKey) {
// Create an async script element for analytics.js based on your API key.
var script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.src = '@@AUTOCHART_CDN_URL/vLatest/autochart.track.min.js';
// Find the first script element on the page and insert our script next to it.
var firstScript = document.getElementsByTagName('script')[0];
firstScript.parentNode.insertBefore(script, firstScript);
window.autochart.init(apiKey);
};
/*!
Replace <YourCustomerAccountIdHere> below with your customer API tracking key
*/
window.autochart.load('<YourCustomerAccountIdHere>');