OT4: Tagging a session and subsequent sessions. How do I get my custom data?
If you want to tag a session with a custom metric then this article is for you. Lets say you want to tag a session with a customerId that comes from another system. This is useful if you want reports on customer's that log into your system.
How do I get the customerId into Opentracker's datastore/ user interface?
Opentracker's script is written to tag the browser and get data into Opentracker's elastic-search datastore. Url's are quickly parsed to "tag" the browser with keys and values. If an "attribute" in the url starts with "ot_" or "utm_" the browser gets tagged. For example this would turn on this behavior for ot_customerId and utm_campaign:
You can also provide variables that do not get "tagged" to the user's browser, but get sent along with an event. In this article we will focus on tagging the user's browser. If this sounds too complicated, Opentracker can provide services to implement solutions for your business needs, as well as making sure you are GDPR compliant, contact us for more info.
A simple example of sending an event with javascript
In the simplest form this is how you send an event:
<script> // run this script when opentracker has loaded function ot_onload() { // map an event ot_map.put("ot_video_title", title); // send the event ot_log_state(); } </script>
A javascript method continued
ot_map.get("ot_customerId");
var myCustomerId = "This is my id"; ot_tag("ot_myCustomerId", myCustomerId); // refresh browser ot_map.get("ot_myCustomerId");<br>
https://stackoverflow.com/questions/1116983/javascript-how-do-i-make-sure-a-jquery-is-loaded
Putting the callback function ot_onload to work
The Opentracker script, once loaded, will immediately check for a function called "ot_onload". This is a hidden function that if defined, gets called. This function needs to be loaded before the script itself and needs to call ot_log_state() - to record all data. The function needs to be in the html of the page, or if loaded externally, needs to be placed before the loading of Opentracker's script. A fully functional example looks like this:
<script> // method called by opentracker's callback on script load, must precede actual loading of script function ot_onload() { var myCustomerId = "12345"; if (myCustomerId) ot_tag("ot_myCustomerId", myCustomerId); // needed to log or send the state of all variables, otherwise things won't get stored on the server ot_log_state(); console.log("ot_myCustomerId: " + ot_map.get("ot_myCustomerId")); } </script> <!-- load the opentracker script with debuging to console.log --> <script src="//script.opentracker.net/?site=REPLACE_THE_SITE"></script>
You will need to replace REPLACE_THE_SITE with the site you registered at Opentracker when starting your account. You can view a working example here. The reporting interface will now show the new custom metric ot_myCustomerId:
Need more information on how to implement things in your business? Schedule a Call