4572 Posts in 1316 Topics by 981 members
Customising the CMS
SilverStripe Forums » Customising the CMS » Google Analytics code as Requirement
Moderators: martimiz, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba
|
Page:
1
|
Go to End | |
| Author | Topic: | 1256 Views |
-
Google Analytics code as Requirement

22 June 2009 at 9:28pm Last edited: 22 June 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!
-
Re: Google Analytics code as Requirement

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. ;-)
-
Re: Google Analytics code as Requirement

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!
-
Re: Google Analytics code as Requirement

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...
-
Re: Google Analytics code as Requirement

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
-
Re: Google Analytics code as Requirement

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
| 1256 Views | ||
|
Page:
1
|
Go to Top |


