Simple Machine Forums User tracking with Google Analytics
Google Analytics gives you the ability to track visitors by custom variables, but I couldn't seem to find much information on it until I knew the right term to search on - hint, it's "__utmSetVar".
The __utmSetVar javascript function lets you set a variable that Analytics will then use in the statistics it keeps. Say you want to keep track of your visitors' gender. You could ask them their gender on the first page load and then run __utmSetVar('Male'); or __utmSetVar('Female'); depending on what they answer and then you could correlate your stats to gender.
While I would be interested in the gender of my visitors, I'm not going to flat out ask because most people wouldn't answer or would hit the back button quickly. Plus, there's more important stuff that I'd like to track.
For one of my forums I wanted to track whether the page views were from members, guests, moderators, or admin. Fortunately Simple Machines makes this fairly easy by exposing a couple of variable arrays that make this fairly easy.
$context and $user_info
The two arrays we'll be using are $context and $user_info. They'll give us information on the visitor to know what group they should fall in. Each of these arrays contains some pretty good information, but beyond the scope of this article. For now just go with what's in the code below.
A little preparation
Before we start, log in to the admin pages on your SMF forum. You're going to need the database ID of the global moderator group and it's a lot easier to get it from the admin pages than directly from the database.
Click on Membergroups on the left side.
Hover your mouse over Global Moderator. Down in the status bar you should see a URL that ends with group=2. Remember that number, although it will probably be 2. If you don't have the status bar in your browser turned on you can also click on where it says Global Moderator. The URL in the address bar after you click will have the same group=2.
Editing the template
The first change we need to make is to add the $user_info array to the scope for the template_main_above() function because we'll be using part of that variable to pick up global moderators. We're also going to need the $context array but it is already defined as a global.
This is lines 60 through 62 on the stock index.template.php file that ships with SMF 1.1.2. It may be different depending on what mods you have installed. Line 4 is the change - we just added $user_info on to the end of the list.
-
// The main sub template above the content.
-
function template_main_above()
-
{
Inserting the code
Next we're going to need to go down to just above the tag in the template. It may be within an echo line, and if so you'll have to put a '; after the previous text and echo '
Now you're going to insert your Google Analytics code and the code to set the user variables. Look for the following code. It is lines 155 through 171 in the stock index.template.php file.
-
if ($context['user']['is_guest'])
-
echo '
-
document.cookie = "upshrinkIC=" + (mode ? 1 : 0);';
-
else
-
echo '
-
smf_setThemeOption("collapse_header_ic", mode ? 1 : 0, null, "', $context['session_id'], '");';
-
-
echo '
-
document.getElementById("upshrink_ic").src = smf_images_url + (mode ? "/expand.gif" : "/collapse.gif");
-
document.getElementById("upshrinkHeaderIC").style.display = mode ? "none" : "";
-
current_header_ic = mode;
-
}
-
// ]]></script>
-
</head>
-
<body>';
And change it to
-
if ($context['user']['is_guest'])
-
echo '
-
document.cookie = "upshrinkIC=" + (mode ? 1 : 0);';
-
else
-
echo '
-
smf_setThemeOption("collapse_header_ic", mode ? 1 : 0, null, "', $context['session_id'], '");';
-
-
echo '
-
document.getElementById("upshrink_ic").src = smf_images_url + (mode ? "/expand.gif" : "/collapse.gif");
-
document.getElementById("upshrinkHeaderIC").style.display = mode ? "none" : "";
-
current_header_ic = mode;
-
}
-
// ]]></script>';
-
?>
-
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
-
</script>
-
<script type="text/javascript">
-
_uacct = "UA-xxxxx-yy";
-
urchinTracker();
-
</script>
-
<?php
-
if ($context['user']['is_guest'])
-
{
-
echo "<script type=\"text/javascript\">__utmSetVar('Guest'); </script>";
-
}
-
elseif ($context['user']['is_admin'])
-
{
-
echo "<script type=\"text/javascript\">__utmSetVar('Administrator'); </script>";
-
}
-
{
-
echo "<script type=\"text/javascript\">__utmSetVar('Moderator'); </script>";
-
}
-
else
-
{
-
echo "<script type=\"text/javascript\">__utmSetVar('Member'); </script>";
-
}
-
echo '</head><body>';
I left the existing code as part of the example so that you could see where the new code fits in.
What's going on
Let's look at what the changes do. This is actually lines 155 through 194 but I'm going to use the line numbers in the code box above to describe everything.
Lines 15 and 16 close the echo command starting on line 8 and then close the PHP parser because it's generally easier to insert Javascript outside of the parser.
Lines 17 through 22 is the code that Google Analytics gives you to insert onto your page. It will probably be very close to what is here aside from line 20 which is your account number and the profile number.
Line 23 reopens the PHP parser so that our code will be run and because it needs to be open for the SMF code below.
Lines 24 through 39 set the __utmSetVar variable to match what type of user they are. Guest and admin users can be checked by using the $context array. Global moderators have to be checked by checking if the database id for global moderator is in their group list. This is the 2 that we talked about earlier, although you may have to change it to match what's in your database.
So, what's it look like?
It's a fairly new forum so my stats aren't impressive at all, but here's what it looks like when you click the User Defined link under Visitors.

The (not set) is from before I started doing this and will stop showing up in reports as they age. The next 4 lines are which visitors were Members, Moderators, Guests, and Administrators respectively. I'd sure like to see a few more Guests, but this is only a few hours of data so I can't make any real assumptions yet.
What else can we do
It's possible to track several different member groups using constructs similar to lines 32 through 35. The only catch is that you can only put the visitor in one member group for tracking purposes. So if they are in member group 2 and 4 then they will only show in one, and it will be the first one in the if, elseif, else list.
Question, Comments...
Do you have more questions. Please either leave a comment below or join us in our new forum.