Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Google Analytics code as Requirement


Go to End


6 Posts   3129 Views

Avatar
SilverRay

Community Member, 167 Posts

22 June 2009 at 9:28pm

Edited: 22/06/2009 9:29pm

Would there be any problem using the Google Analytics script by way of the init method? Like:

Requirements::javascript('mysite/javascript/google.js');

It seems btw, that 2.3.2 changed the way javascript gets called this way? When I upgraded, my called scripts via the Requirements method were placed in between the two javascript blocks of the little Google analytics code... anybody seen that too?

Thanks!

Avatar
Sam

Administrator, 690 Posts

23 June 2009 at 7:51pm

Putting <script> tags in the head isn't good for efficiency, because the page has to wait to load all the JS before it's displayed. So we've updated Requirements to include the scripts at the end of the body.

If, however, you already have a script tag in your body, then it will put the Requirements JS just before that script, just in case your script depends on one of the Requirements script tags.

Including Google Analytics via Requirements::javascript() should be fine; give it a go and see if it works. ;-)

Avatar
SilverRay

Community Member, 167 Posts

23 June 2009 at 8:36pm

Thanks for your reply! Right, I had already some scripts before the closing body tag. I will give it a try with including Google Analytics through Requirements.

Thanks!

Avatar
SilverRay

Community Member, 167 Posts

6 March 2010 at 3:26pm

OK, so to follow up on this (late, I know): including Google Analytics via Requirements::javascript() does not work very well it seems. Only very few visits are picked up, 1 or 2. When I revert back to putting the GA code straight in the page template, it works normally again. I think it has something to do with how Google sees the script in the page? Strange though...

Avatar
salbelle

Community Member, 6 Posts

30 March 2010 at 10:59am

Hi there,

I'm completely new at Google Analytics and I'm trying to get the code into the HTML in the Silverstripe CMS but it doesn't seem to read it. I can't quite understand what you gauys are talking about here (don't laugh!) so could you please maybe simplify it for me?

Thanks :)

Avatar
SilverRay

Community Member, 167 Posts

30 March 2010 at 6:41pm

OK, so at the end of the HTML code in your Page template, put in your google code, like so:

...
<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("XX-YYYYYYY-Z");
pageTracker._trackPageview();
} catch(err) {}</script>
</body>

</html>

where XX-YYYYYYY-Z is your own tracker...

HTH