Skip to content

Relating a WT1 visitor to your internal users

Arnaud PICHERY edited this page Oct 19, 2018 · 2 revisions

Through the concept of visitor, WT1 provides website developers, out of the box, with the ability to track the actions of a unique visitor (ie, browser or mobile device) consistently, accross sessions and time.

A very common additional need of website developers is to correlate the activities of a visitor on a webpage to a user in the Website's backend system.

It can be required for example:

  • To follow the actions of one user accross different browsers and devices (think computer, smartphone, tablet);
  • To correlate the actions of the user with the profile that the website has in its database;
  • To cross the actions of the user with the orders / baskets in the billing system
  • ...

This can be achieved using Custom variables

User arrives on the website

  • On the first interaction of the user on the website with a new browser, the user is not logged in.
  • On the "home page", a WT1 page tracker is installed : _wt1Q.push(["trackPage"])
  • WT1 assigns to the visitor a visitor id, which will remain constant for all interactions of this visitor.
  • In this tracking event, the visitor id will not be related to a system user

User logs in

  • User arrives on the login page. If the login page has a tracker, it will create another tracking event without system user
  • The backend identifies the user and returns in the generated HTML code either
    • a fully built JS snippet that will set the custom variable
    • a JS snippet that sets a JS variable with the user name
  • The JS code executes a set of the visitor-scoped custom variable containing the system user name
  • The JS code sends a tracking event to "push" the custom variable to the backend (and also to indicate a successful login)

From now on, all interaction from this visitor will contain as a custom variable the system user name/id, making all correlations easy.

Sample

Here is a very simple sample of how a PHP backend could implement the visitor-to-user mapping.

Home page

<script type="javascript">
var _wt1Q = _wt1Q || [];
(function() {
  var script = document.createElement('script');
  script.src = "/track.js";
  script.type = 'text/javascript';
  script.async = "true";
  var script0 = document.getElementsByTagName("script")[0];
  script0.parentNode.insertBefore(script, script0);
})();
 
_wt1Q.push(["trackPage"]);
</script>
 
<h1>Welcome on the home page</h1>

Login page

<script type="javascript">
var _wt1Q = _wt1Q || [];
(function() {
  var script = document.createElement('script');
  script.src = "/track.js";
  script.type = 'text/javascript';
  script.async = "true";
  var script0 = document.getElementsByTagName("script")[0];
  script0.parentNode.insertBefore(script, script0);
})();
_wt1Q.push(["trackPage"]);
</script>
</head>
 
<body>
 
<?php
  $user = $_POST["user"];
  $pass = $_POST["pass"];
 
  if (password_matches($user, $pass)) {
    /* User is now logged in, so insert the custom variable in the returned code */
    echo '<script type="javascript">_wt1Q.push(["setVisitorParam", "internal_user", "'.$user.'value"]);';
    echo '_wt1Q.push(["trackEvent", {"type" : "successful_login" }]);';
  } else {
    die "Go away, you pirate, you are not $user";
  }
?>