Hybrid Analytics

14 Jul 2019 » Analytics Tips , Server Side

The last tool I showed in my Summit lab was Adobe Analytics. Initially, I was not sure whether I should write about it. However, I have come up with some ideas to share and here you have the final post on this lab.

If you have not read it, I recommend that, before you continue with post, you first read my post on server-side Adobe Analytics. In it, I explained 2 options you have to make the calls (I later learned there are 3 options). In my sample PHP Analytics class, I used the GET method, but feel free to use the equivalent POST method. What I do not recommend is you use the XML-based POST method, as it does not support A4T.

Second page onwards

As opposed to the other tools I have explained, I will start with the case of the AMCV cookie already present. Remember that this only happens on the second page of a visitor. The first page is always special and needs a special treatment.

In the sample index.php page I used in the Summit, you can see the following code:

/* Initialise Adobe Analytics */ 
$analytics = $ecid_available ? new Adobe\Analytics($config,$ecid,$data_layer['user_agent'],$data_layer['ip_client']) : null;

// ...

if ($analytics) :
  $analytics->setPageName($data_layer['page_name']);
  $analytics->setURL($data_layer['url']);
  $analytics->setEvar(1,$data_layer['page_name']);
  $analytics->setProp(1,$data_layer['url']);
  $analytics->setProp(2,'Server-side');
  $analytics->setEvent(1);
  if (isset($data_layer['referrer'])) {
    $analytics->setReferrer($data_layer['referrer']);
  }
  if (isset($mbox)) {
	$analytics->setTnta($mbox->getTnta());
  }
  $hit_url = $analytics->sendHit();
else :

The initialisation happens at the top of the page, whereas the tracking is at the bottom of the page. I will come back to this later.

As usual, if we are on the first page of the first visit, $ecid_available will be FALSE and $analytics will be null. Only from the second page onwards, this variables will be useful. The Adobe\Analytics class can be done in multiple ways. In my example, I just have various Adobe\Analytics::set* methods to set the various variables and, once the object is ready, I call Adobe\Analytics::sendHit(), which will make the GET call to the Adobe Analytics servers. It is a simple as this and I not think it requires any further explanations. Let me know in the comments if you need any clarifications.

First page of the first visit

As usual with a hybrid implementation, the first page is a special case. You cannot execute it server-side, as the ECID object has not been initialised. In my example, I show a simple solution: generate the client-side call as we use to do it 5 years ago:

<script type="text/javascript" src="script/AppMeasurement.js"></script>
<script type="text/javascript">
  var s = s_gi("<?php echo $config->getAnalyticsConfig(Adobe\Config::ANALYTICS_RSIDS) ?>");
  s.visitor = visitor;
  s.trackingServer = "<?php echo $config->getAnalyticsConfig(Adobe\Config::ANALYTICS_SERVER) ?>";
  s.charSet = "<?php echo $config->getAnalyticsConfig(Adobe\Config::ANALYTICS_CHARSET) ?>";
  s.pageName = "<?php echo $data_layer['page_name'] ?>";
  s.eVar1 = "D=pageName";
  s.prop1 = "D=g";
  s.prop2 = "Client-side";
  s.events = "event1";
  s.t();
</script>

In order to configure the s object, I use the Adobe\Config class, so that both server- and client-side codes get exactly the same parameters. Then, you need to mimic in JavaScript the same server-side calls to set eVars, props, events and all other parameters the s object may need. Having a server-side data layer becomes very handy, as you just need to reference the values you already have. Do not forget to call s.t()!

Alternative solutions

The previous code is just one of the many options, in which Analytics can be deployed server- and client-side.

Call Analytics server-side at the top of the page

Nothing prevents you from putting the server-side code at the top of the page. You may want to spawn a thread to take care of this task, so you can continue generating the HTML as fast as possible. Otherwise, a blocking call will void one of the advantages of server-side digital marketing: faster websites. In the case of client-side code, you should still add the JavaScript code at the bottom of the page.

Use a tag manager

I do not recommend this solution for various reasons:

  • You need to keep in sync the server-side code and the tag manager configuration. This is very likely to fall out of sync very quickly.
  • You need to add additional code to load Adobe Analytics only on the first page of the first visit.
    • If you, accidentally, load and execute the Analytics code after this first page, you will be counting twice everything: page views, eVar instances, events… Not to mention that the values may be different, ruining your reports. And you will be paying additional server calls!
    • Even if the code is loaded but not executed, you are slowing down the page.

However, if you insist, go ahead and do it. If you are forced to use this option, make sure everybody understands the risks.

Add an additional call to your servers

One final idea I had was to make a call from the browser to the your servers. This can be one these two options:

  • An image. The URL should a server endpoint (e.g. a servlet in Java) that executes the Analytics code and return a transparent pixel. This image can be loaded without actually putting it in the DOM, so the website is not affected.
  • An AJAX call. As with the previous point, the endpoint executes the Analytics code and returns nothing.

The general suggestions to any of these calls is to put them at the bottom of the page. At this point in the execution, the AMCV cookie should already be present, so the call will carry it. To me, the main advantage and beauty of this idea is that you always use the same server-side code, with no need of any Analytics client-side code, other than the call to the server endpoint.

Photo by Stephen Dawson on Unsplash



Related Posts