Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Insert Google Analytics, but not for Admins
#1
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 Code:
<?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?
Reply
#2
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.


Attached Files
.php   ganalytics.php (Size: 3.47 KB / Downloads: 47)
Reply
#3
(05-24-2011, 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.
Reply
#4
It is working fine here.

PHP Code:
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.
Reply
#5
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!
Reply
#6
(05-24-2011, 11:15 AM)freakenstein Wrote:  w00t, thanks!

Great! Glad that I could help.
Reply
#7
this is new Smile
["_trackPageLoadTime"]
Hello, Welcome to MyBB Indonesia to get local support
My 'Simple' Unique Plugin here Smile
Reply
#8
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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)