Understanding Lytics / Product Documentation / User Profiles

Reading Entity via Server

By default, Lytics returns a users profile and any whitelisted fields to the JavaScript to the browser. This, however, is not always sufficient when it comes to marketing use cases. An alternative approach is to do the lookup based on cookie using your server side language of choice.

For the approach we will be doing the following:

  1. Reading the browsers cookies to get the visitor's _uid in order to identify them.
  2. Making a call to the Lytics Personalization API to get the visitors current profile.
  3. Surfacing that data to the browser as a JavaScript object.

Identifying the Visitor

Identifying the user can be done a variety of ways. By default, Lytics sets a cookie seerid that is used to identify a user. This is then surfaced in a users profile as _uid. Out of the box, this is what our JavaScript tag uses for web based identity resolution. That said, if your users are logged in or identified by another known key this can be used in place of our seerid in the following examples.

Since we already have a cookie all we need to do is read those so that we can get the users profile from our API.

$_COOKIE["seerid"];

Get Visitors Profile

Once we have an identifier we need to make a GET request to our personalization API. In our example we are taking the cookie value and building a simple call to our api that asks for three fields: last_make, last_model, last_color as well as audiences which are returned by default. If using a custom key/value for identification you simply replace the $fieldname with the name of your identifier in Lytics and $value with the value of that key.

<?php
  $fieldname = "_uid";
  $value = $_COOKIE["seerid"];
  $apitoken = "{yourapitoken}";

  $url = "https://api.lytics.io/api/entity/user/".$fieldname."/".$value."?fields=last_make,last_model,last_color&key=".$apitoken;
  $data = json_decode(file_get_contents($url), true);
  $profile = $data["data"];
?>

Surface Data to Browser

Finally we simply parse that response and either inject the variables directly into our template or surface the profile as a JavaScript object.

<?php
  $fieldname = "_uid";
  $value = $_COOKIE["seerid"];
  $apikey = "{yourapitoken}";

  $url = "https://api.lytics.io/api/entity/user/".$fieldname."/".$value."?fields=last_make,last_model,last_color&key=".$apitoken;
  $data = json_decode(file_get_contents($url), true);
  $profile = $data["data"];
?>

<html>
<head>
  <script type="text/javascript">
    window.jstag=function(){function t(t){return function(){return t.apply(this,arguments),this}}function n(){var n=["ready"].concat(c.call(arguments));return t(function(){n.push(c.call(arguments)),this._q.push(n)})}var i={_q:[],_c:{},ts:(new Date).getTime(),ver:"2.0.0"},c=Array.prototype.slice;return i.init=function(t){return i._c=t,t.synchronous||i.loadtagmgr(t),this},i.loadtagmgr=function(t){var n=document.createElement("script");n.type="text/javascript",n.async=!0,n.src=t.url+"/api/tag/"+t.cid+"/lio.js";var i=document.getElementsByTagName("script")[0];i.parentNode.insertBefore(n,i)},i.ready=n(),i.send=n("send"),i.mock=n("mock"),i.identify=n("identify"),i.pageView=n("pageView"),i.bind=t(function(t){i._q.push([t,c.call(arguments,1)])}),i.block=t(function(){i._c.blockload=!0}),i.unblock=t(function(){i._c.blockload=!1}),i}(),window.jstag.init({cid:"{yourcid}", url:"//c.lytics.io", min:true, loadid:false});

      var lyticsProfile = <?php echo json_encode($profile); ?>;
  </script>
</head>
<body>
  Make: <?php echo $profile["last_make"]; ?><br/>
  Model: <?php echo $profile["last_model"]; ?><br/>
  Color: <?php echo $profile["last_color"]; ?>
</body>
</html>