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

2 comments:

Robert said...

Thanks for putting this together. Very helpful.

commission-surendettement said...

I, I've tried you tutorial but i don't have the same directorie...
mine is /www/forum/Sources/ga_script.html

then I insert the code inside index.template.php as following :


// Add in google analytics
require_once 'ga_script.html';

echo '
code lines ( code is not allowed)
head
body


But it doesn't run. I can save, but no results in google analytic... any idea ??

thank you for your help