Jump to:

5450 Posts in 1672 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Google Analytics code as Requirement

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

Page: 1
Go to End
Author Topic: 1724 Views
  • SilverRay
    Avatar
    Community Member
    167 Posts

    Google Analytics code as Requirement Link to this post

    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!

  • Sam
    Avatar
    Administrator
    679 Posts

    Re: Google Analytics code as Requirement Link to this post

    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. ;-)

  • SilverRay
    Avatar
    Community Member
    167 Posts

    Re: Google Analytics code as Requirement Link to this post

    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!

  • SilverRay
    Avatar
    Community Member
    167 Posts

    Re: Google Analytics code as Requirement Link to this post

    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...

  • salbelle
    Avatar
    Community Member
    6 Posts

    Re: Google Analytics code as Requirement Link to this post

    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

  • SilverRay
    Avatar
    Community Member
    167 Posts

    Re: Google Analytics code as Requirement Link to this post

    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

    1724 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.