How to add Google Analytics code?

GK User
Fri Aug 26, 2011 12:17 pm
I would like to add Google Analytics code but when I editing html (index.php) I can not see </body> tag. 3rd party modules also needs to add some code before </body> tag. How to add this code?
User avatar
Senior Boarder

GK User
Fri Aug 26, 2011 1:06 pm
Have you tried adding to layouts/default.php ?

I prefer to put the code into its own php file (in this example named analytics.php) and call it using the code:

Code: Select all
<?php include_once("analytics.php") ?>


Immediately after the body tag.

There is a different analytics code available from Google for iphone and android, you can add these into the appropriate files also.
User avatar
Senior Boarder

GK User
Fri Aug 26, 2011 2:16 pm
Laus, thank you for your help. I made the analytics.php file and I added the code line you provide into default.php.

Should I make another php files with analytics for iPhone and Android like analytics_iphone.php and analytics_android.php? I can not find on Google Analytics codes for iPhone and Android.

This line
Code: Select all
<?php include_once("analytics.php") ?>
should be after the <body> or before </body>? Analytics advise to add this code before </head> tag.
User avatar
Senior Boarder

GK User
Fri Aug 26, 2011 4:12 pm
According to Google there is no option to do the same with the PHP include, so I have included the code they provide on iphone.php and handheld.php

It seems to be working for me.

I can't include the code that is required for these so I suggest you go to google analytics and generate a new account (use a bogus sub domain if ness) then when you get to the Tracking Code page, select the "advanced" tab and click on the option for "A site built for a mobile phone" - the instructions are quite thorough.

Within the code you will see the account id looking something like "MO-12345678-1" if you then substitute this account code for the one you already have, leaving the 'MO' at the front instead of 'UA' then you should be fine.

I think there are three steps to it, including a php file to add into the layouts folder. Good Luck.
User avatar
Senior Boarder


cron