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!
}
– TryHarder Apr 5 '17 at 23:23