Monday, December 8, 2008

Simple Machines Forum Software and Google Analytics

Simple Machines provides free Forum software that works, is flexible, easy to install and use.

Applying design themes is one of its most powerful features because it uses templates processed by PHP.

But therein lies the stumbling block that trips up forum owners wanting to install Google Analytics code.

The approach followed here is the least invasive, most flexible and allows for more advanced functionality, if and as required.

3 steps:

Step 1: Create an include file for your Google Code.

Paste the following code in an empty text file and save it in /smf/Sources as /smf/Sources/ga_script.html
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-#######-#");
pageTracker._trackPageview();
} catch(err) {}</script>

Replace UA-#######-# with your Web Property ID from your web analytics account.

Step 2: Find the correct index.template.php

Find your theme directory. Typically it will be the theme folder beneath /smf/Themes.

Open the file /smf/Themes/<your theme>/index.template.php

In my case, I've been totally original in going with the "classic" theme so I'm using
/smf/Themes/classic/index.template.php

Step 3: Insert on line of code before the closing </body> tag
Open index.template.php in a text editor.

Search for </body>
You will find the following lines of code:

echo '
</body>
</html>';

Before the echo statement insert the following comment and one line of code:
// Add in google analytics
require_once 'ga_script.html';

Save the file and you're done.

Testing.
  • Go to your forum.
  • Delete your cookies for your forum's domain.
  • Refresh the page.

View your cookies. You should have, amongst others, the following 4:
  • __utma,
  • __utmb,
  • __utmc and
  • __utmz
For more on GA's cookies, see:
Cookies Set By Google Analytics
or, for the real geeks:
Slicing and Dicing Google Cookies - Part 1

After 2 hours, log into your Google Analytics account and check your reports - you should be seeing some data.

No doubt you have questions - I'll probably have answers or know someone who has.
No doubt you want more - ask and you may very well receive.

Questions us forum owners should be asking include:
  • Why do I have a forum and what stats will tell me if its meeting those objectives?
  • What do I want visitors to do on my forum and how do I know if and how much they are doing of it?
  • What do I want more of on my forum? What stats will point me to getting more of that?

Brian

13 comments:

Robert said...

Thanks for putting this together. Very helpful.

Anonymous said...

wow you created a whole blog for this?? what an idiot.

Anonymous said...

Thank you! very helpful!

Anonymous said...

Hi, I am trying to follow your instructions but there is a whole bunch of stuff between echo' and "body/html" (stuff referencing ajax and ie7) - I am also using the classic theme. So, I'm not sure where exactly to put the ga require_once statement. Can you post an example? Thanks!

Yuechen Zhao said...

Hey, thanks man! Very easy and very useful.

Anonymous said...

Thanks this is really helped me.

Anonymous said...

I tried this but I don't see /body in the index.template.php file. The last few lines in the file are

$custom_td, implode($context['menu_separator'], $button_strip)

There are some td tags around it and all but blogger won't let me include them in the comments

Anonymous said...

This was very helpful for me. Thanks very much for posting!

K_Kahne_Fan said...

Wouldn't it be easier to use the Global Headers and Footers tag, then just put the code in your Global Footer?

http://custom.simplemachines.org/mods/index.php?mod=351

Anonymous said...

Thank you!

Jan-Erik, Finland said...

Since I most likely will forget to come back here and say thank you when the Analytics firework go off in a few hours, I'll thank you right away. Very helpful for a non-nerd like me.

Anonymous said...

Quote: Hi, I am trying to follow your instructions but there is a whole bunch of stuff between echo' and "body/html" (stuff referencing ajax and ie7) - I am also using the classic theme. So, I'm not sure where exactly to put the ga require_once statement. Can you post an example? Thanks!just place the line before " // The following will be used to let the user know that some AJAX process is running
"
that should do it.

Anonymous said...

Just wanted to say a quick thanks for making this process clear and precise. I'm now google tracked. thanks mate.