JavaScript

The Journey Analytics JavaScript Tracker gives you full control over events sent from your website, by defining default setting (init), sending custom events, and more advanced options.

Initialization

The init function initializes a new instance of the CooladataTracker object.To start tracking with Journey Analytics JavaScript Tracker, add the following code to the header of each page you want to track (between the <head> and the </head> tags) and replace the app key with your own:

Note: On WordPress sites, add the snippet in the header.php file. It can usually be found in the wp-admin under appearance -> Editor -> theme header(header.php).
<!-- Start Cooladata -->
<script type="text/javascript">
    (function(d,a){if(!a.__SV){var b,c,g,e;window.cooladata=a;a._i=[];a.init=function(b,c,f){function d(a,b){var c=b.split(".");2==c.length&&(a=a[c[0]],b=c[1]);a[b]=function(){a.push([b].concat(Array.prototype.slice.call(arguments,0)))}}var h=a;"undefined"!==typeof f?h=a[f]=[]:f="cooladata";g=["trackEvent","trackEventLater","trackPageload","flush","setConfig"];for(e=0;e<g.length;e++)d(h,g[e]);a._i.push([b,c,f])};a.__SV=1.2;b=d.createElement("script");b.type="text/javascript";b.async=!0;b.src="https://cdn.cooladata.com/tracking/cooladata-latest.min.js";c=d.getElementsByTagName("script")[0];c.parentNode.insertBefore(b,c)}})(document,window.cooladata||[]);

    cooladata.init({
        "app_key": "<YOUR_APP_KEY>", //replace with the app key, found under "Event Sources" in the Cooladata project menu
        "track_pageload": true,
        "img_src_get_request": true
    });

</script>
<!-- End Cooladata -->

The above setup includes the recommended implementation options:

  • Track page views automatically
  • Uses a tracking pixel

The following table includes all configuration options:

app_keyStringYesThe specific project app key provided by CoolaData
http_postBooleanNoFalseUse HTTP POST method instead of the default GET method
img_src_get_requestBooleanNoFalseSend an image (1x1 pixel) as GET request for XHR requests
disable_cookieBooleanNoFalseDisables cookie creation on the user browser when tracking an event. When true, the user ID sent by Journey Analytics will change with every page load, and user identification management will be dependent on the user ID sent in the event tracker ("user_id" in the tracker, "customer_user_id" in CQL).
api_hostStringNoapi.cooladata.comReplace the default API domain with a custom domain as the tracker endpoint. For example: "api_host":"tracking.com" will be sent to: http://tracking.com/v1/ [app_key]/track
track_pageloadBooleanNoFalseSend page_load event automatically every time a user views the page. Sent with the following properties: page_url, page_url_params, page_title.
Note:

The track_pageload parameter is sent together with the user_id. If no user_id was explicitly specified in the cooladata.init() function, a random user_id will be generated and used for each event. Pay attention that if you DON'T send a user_id in the init function but send one in the cooladata.trackEvent() both random and custom user_ids will not be associated with each other.

user_idStringNoFalseSpecify the user_id on the page's cooladata tracker init to associate all events (including track_pageload) to the current user.

Track Custom Events

Once you have the Tracker snippet in your page, you can track an event by calling cooladata.trackEvent(); with the event name and its properties:

How to use:

cooladata.trackEvent('<event_name>',{'<property_name1>':'<property_value1>', '<property_name2>':'<property_value2>'});

The function is built of a few parts:

  • The function call: cooladata.trackEvent()
  • The event name
  • The properties & their values a JSON. The JSON must be "flat" so you can't use any nested JSON.

Example:

cooladata.trackEvent('Add Item', {'Type': 'T-Shirt', 'Amount': 2, 'email':'sam@email.com'});

Each event can include properties. For example, the above 'Add Item' event includes the item type, number of items, and the purchaser's email address. Read more about properties.

You could also use JQuery to send an event to the Cooladata server. In this example a function is called when a form's SEND button is clicked (this example uses ninja forms). JQuery is used here to extract the data from the different form's fields.

jQuery(document).ready(function($){
    var sendButton = $("#ninja_forms_field_51"); //the form object sending the form (send button)
    sendButton.click(function(){
        callTrack4();
    });
});

function callTrack4(){
    var name_var = $("#ninja_forms_field_45").val(); //form variables for properties
    var lastName_var = $("#ninja_forms_field_46").val();
    var email_var = $("#ninja_forms_field_47").val();
    var phone_var = $("#ninja_forms_field_48").val();
    var property_value_var = $("#ninja_forms_field_49").val();
    cooladata.trackEvent('secondPopup', {'name': name_var, 'last_name':lastName_var, 'email':email_var , 'phone':phone_var,'property_value':property_value_var}); //CoolaData trackEvent
}

In the table below you can find all cooladata.trackEvent() elements (some are optional):

eventNameStringYesThe name of the event to report.
eventPropertiesObjectNoObject containing all additional properties in format: "property_name":"property_value". See example below.
user_idStringNoSend if you want to be able to identify users according to your definition for user ID. For example: use this for cross-platform identification where users perform a log-in action.
event_timestamp_epochNumberYes**Must be sent if the project is NOT set to override event time-stamp with server time.Format is milliseconds (13 digits) only. If the project is defined to override event time-stamp with server time, any time-stamp sent with the event will be ignored.
custom propertiesString/NumberNoAny additional properties mapped in the project. See more information in Managing Properties.

Use a callback function

In some cases, after sending an event to the server, you would want to execute some code on the Browser. This can be easily done by adding a callback function to your cooladata.trackEvent() call.

The process works as follows: Tracker sends event to Journey Analytics server -> Journey Analytics server sends OK response to Tracker -> callback function is executed.

How to use:

cooladata.trackEvent('<event_name>',{'<property_name>':'<property_value>'},function(){
  //your callback function code here
});

Example:

cooladata.trackEvent('get_coupon',{'discount':'35%',function(){ 
  window.alert('You've got 35% discount!') 
});

Track a bulk of events

The cooladata.trackEventLater() method provides the ability to track a bulk of events. All events will be stored on a local queue until the cooladata.flush() method is called. This method can be useful when you want to submit more than one event at a time, for example to send a list of events that are linked together all in a single request, instead of multiple requests.

Example:

  1. Add an event to the queue:

    cooladata.trackEventLater("register_submit",{"user_id":"JohnDoe", "subscription_active":0})
  2. Add a second event to the queue:

    cooladata.trackEventLater("add_to_cart",{"user_id":"JohnDoe", "sku":JSV-4532})
  3. Send the events to the Cooladata server

    cooladata.flush();