1

My Joomla template includes the analyticstracking.php file with the tracking script, which has worked great, however I'd like to implement Google Analytic's userID feature (for cross-device tracking) for logged in users. Here's the original, working analyticstracking.php file (essentially just the basic analytics script):

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  ga('create', 'UA-XXXXXXXX-X', 'auto');
  ga('require', 'linkid', 'linkid.js');
  ga('require', 'displayfeatures');
  ga('send', 'pageview');
</script>

After doing some research, I came up with the following php script to try to generate the userID from Joomla's built-in $user->id, if the user is logged in:

<?php 
   $user = JFactory::getUser();
   if ($isset($user->id)) {
    $gacode = "ga('set', 'userID', '%s');";
    echo sprintf($gacode, $user->id);
?>

I tried just inserting that into the javascript, but that crashed my site. So I don't know whether my php is wrong, or if I can't add it to the script in that way, or something else entirely? Please help!

  • It's always a good idea to check your error logs to see what the actual error is. I've only taken a quick look at your code, but it looks like you're missing a curly bracket } – TryHarder Apr 5 '17 at 23:23
3

oooooo and Terry Carter both mentioned errors in your code. Besides fixing those it would be good to modify your question to show exactly what your final analyticstracking.php file looks like. Something like this:

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  ga('create', 'UA-XXXXXXXX-X', 'auto');
  ga('require', 'linkid', 'linkid.js');
  ga('require', 'displayfeatures');
  ga('send', 'pageview');

  <?php 
  $user = JFactory::getUser();
  if ( !$user->isGuest && is_numeric($user->id) )
  {
    echo sprintf("ga('set', 'userId', '%d');", $user->id);
  }
  ?>    
</script>

That code has my suggested corrections so you can give it a try.

  • That did it! Thank you so much! – Sam Apr 7 '17 at 13:55
  • There is no need for the is_numeric($user->id) check. If the user is not a guest the id will always return a numeric value greater than 0. – Terry Carter Apr 7 '17 at 15:55
  • Also, in case anyone references this in the future for the same purpose, I realized afterwords that the userID part of the Analytics script must be written as userId for Google to recognize, so the line in the code above should be echo sprintf("ga('set', 'userId', '%d');", $user->id); – Sam Apr 11 '17 at 17:09
  • I've updated the "userId" value in my answer to reflect your note, Sam. – Drew G Apr 20 '17 at 15:01
2

Your $isset line is probably where your issue lies; that is a variable declaration which is wrong.

You may want to do this: is_set ($user->id)

It isn't a great approach either.

Use the isGuest method to return a boolean as to whether or not the user is logged into the site like so:

$user  = JFactory::getUser();
$guest = $user->isGuest;

if (!$guest){
    $gacode = "foo";
}

To add this to your script try the following:

if(!$guest){
    $gacode = "<script type='text/javascript'>_gaq.push(['_setCustomVar','User ID','".$user->id."',3]);</script>";
    echo $gacode;
}

This code should be added after your analytics javascript. If the user is logged in it will then asynchronously push the user id to google analytics using the custom variable line. To read more about custom google analytics variables please see this article.

  • Thanks! But how do I combine the php that retrieves the ID and the javascript that calls Analytics? The javascript is in the php file but it doesn't seem to work when I add the php directly to the script. – Sam Apr 6 '17 at 13:31
  • Please be sure to select an answer to help keep the unanswered questions queue cleared. – Terry Carter Apr 11 '17 at 13:27

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.