Current time: 07-30-2014, 05:07 AM Hello There, Guest! (LoginRegister)


Post Reply 
 
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Insert Google Analytics, but not for Admins
05-24-2011, 09:25 AM (This post was last modified: 05-24-2011 10:01 AM by freakenstein.)
Post: #1
Insert Google Analytics, but not for Admins
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?
Find all posts by this user
Quote this message in a reply
05-24-2011, 10:09 AM (This post was last modified: 05-24-2011 10:09 AM by Aries-Belgium.)
Post: #2
RE: Insert Google Analytics, but not for Admins
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 File(s)
.php  ganalytics.php (Size: 3.47 KB / Downloads: 40)

My latest plugins: Extra Forum Permissions | Facebook Meta Tags | Auto Unapprove Reported Posts
[Image: sig.png]
Find all posts by this user
Quote this message in a reply
05-24-2011, 10:53 AM (This post was last modified: 05-24-2011 11:05 AM by freakenstein.)
Post: #3
RE: Insert Google Analytics, but not for Admins
(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.
Find all posts by this user
Quote this message in a reply
05-24-2011, 11:08 AM
Post: #4
RE: Insert Google Analytics, but not for Admins
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.

My latest plugins: Extra Forum Permissions | Facebook Meta Tags | Auto Unapprove Reported Posts
[Image: sig.png]
Find all posts by this user
Quote this message in a reply
05-24-2011, 11:15 AM
Post: #5
RE: Insert Google Analytics, but not for Admins
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!
Find all posts by this user
Quote this message in a reply
05-24-2011, 11:17 AM
Post: #6
RE: Insert Google Analytics, but not for Admins
(05-24-2011 11:15 AM)freakenstein Wrote:  w00t, thanks!

Great! Glad that I could help.

My latest plugins: Extra Forum Permissions | Facebook Meta Tags | Auto Unapprove Reported Posts
[Image: sig.png]
Find all posts by this user
Quote this message in a reply
05-24-2011, 05:21 PM
Post: #7
RE: Insert Google Analytics, but not for Admins
this is new Smile
["_trackPageLoadTime"]

New Beta Portal from MyBB Indonesia to get local support
My 'Simple' Unique Plugin here Smile
Find all posts by this user
Quote this message in a reply
05-24-2011, 06:37 PM
Post: #8
RE: Insert Google Analytics, but not for Admins
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
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)

Contact Us | MyBB | Return to Top | Return to Content | Lite (Archive) Mode | RSS Syndication