MyBB Community Forums

Full Version: Insert Google Analytics, but not for Admins
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I can up with an idea, which is easy with PHP,
but could found how I should implement this in MyBB.

idea:
I want to insert Google Analytics code for all users, except for the Admins.

In php it would be:
<?php
if ($mybb->user['cancp']) {
echo '<!-- no analytics for Admins -->';
} else {
echo '<script>analytics code blablabla</script>';
}
?>


Yes, I could install the PHP-in-Templates plugin,
but isn't there another option?
I made a small modification to this plugin: http://mods.mybb.com/view/google-analytics

Copy the file in the attachment to your /inc/plugins/ folder, activate the plugin and go to AdminCP > Configuration > Settings > Google Analytics. Enter your Analytics ID and the usergroups you wish to exclude. For admins only, just enter 4.
(2011-05-24, 10:09 AM)Aries-Belgium Wrote: [ -> ]I made a small modification to this plugin: http://mods.mybb.com/view/google-analytics

Copy the file in the attachment to your /inc/plugins/ folder, activate the plugin and go to AdminCP > Configuration > Settings > Google Analytics. Enter your Analytics ID and the usergroups you wish to exclude. For admins only, just enter 4.

Thanks! That was fast!

Seems it's not perfectly working yet, but it's a start.
It is working fine here.

function ganalytics($page)
{
	global $mybb;
	$exclude_groups = !empty($mybb->settings['ganalytics_excludegroups'])
						? explode(",", $mybb->settings['ganalytics_excludegroups'])
						: array();
	if($mybb->settings['ganalytics_webpropertyid'] && !in_array($mybb->user['usergroup'], $exclude_groups))
	{
		$page=str_replace('</head>','<script type="text/javascript">

    window.google_analytics_uacct = "'.$mybb->settings['ganalytics_webpropertyid'].'";
</script>
<script type="text/javascript">
    var _gaq = _gaq || [];
    _gaq.push(["_setAccount","'.$mybb->settings['ganalytics_webpropertyid'].'"]);
    _gaq.push(["_trackPageview"],["_trackPageLoadTime"]);
    (function() {
        var ga = document.createElement("script"); ga.type = "text/javascript"; ga.async = true;
        ga.src = ("https:" == document.location.protocol ? "https://ssl" : "http://www") + ".google-analytics.com/ga.js";
        var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(ga, s);
    })();
</script></head>',$page);
	}
	return $page;
}

The code will not show up for the groups that are excluded in the settings.
Doh! Slap me in my face with a large trout! ... I got logged out on the timer when I was working on the code...

So yes, it does work and it also works with a asynchrone Analytics Code.
w00t, thanks!
(2011-05-24, 11:15 AM)freakenstein Wrote: [ -> ]w00t, thanks!

Great! Glad that I could help.
this is new Smile
["_trackPageLoadTime"]
That's one of the reasons why I converted it to asychronous,
with the new version of analytics you'll find speed information in
content -> site speed Smile