Silverstripe: How to insert Google Analytics code (or any other custom code) at the end of a head section?

Tags: , , ,

By default Silverstripe CMS doesn't have any means to place a custom javascript snippet at the very end of the head section just before "</head>" closing tag. I faced with this today because I wanted to put Google Analytics tracker code there. Of course, I know that technically one can place this code anywhere, but I had problems with my statistics and I wanted to be sure that I did everything as Google said.

Of course you know how to obtain Google Analytics code, but I'll remind you anyway:

  1. Go to analytics settings page, where profiles of the sites you are tracking are listed: https://www.google.com/analytics/settings/
  2. Click "Edit" link which brings you to profile settings page
  3. Click "Check Status" link which opens a window with tracker code from where you can finally copy it

With the following snippet you can place any custom code into the head section very close to an end-tag. The code does it on one of the late stages of the execution, so there are small changes that other code will write something after you:

# mysite/code/Page.php:

class Page_Controller extends ContentController {
    /* other methods have been skipped */
    public function getViewer($action) {
        Requirements::insertHeadTags(<<<EOF
<script type="text/javascript">
    alert('Put your code here!');
</script>
EOF
);		
        return parent::getViewer($action);
    }
}

Yep, I just overloaded Controller::getViewer() in my Page_Controller and did my stuff just before calling the parent method.

If it's not very important to you whether a javascript code is placed at the end of the head section or not, then you can just use the official way for managing javascripts in Silverstripe CMS:

# Page::init():
Requirements::customScript('alert("Put your code here!")');

Also if you're intrested in attaching a javascript file to your page, then this PHP snippet can help:

# Page::init():
// All script-files are relative to the site root:
Requirements::javascript('mysite/javascript/script.js');
// load a js-file from the current theme
Requirements::javascript('themes/' . SSViewer::current_theme() . '/javascript/script.js');

The previous two examples will put your javascript into the body html-section, because Requirements class puts it there by default. In order to place your script somewhere in the head section you need to execute the following PHP code:

Requirements::set_write_js_to_body(false);

I think it's wise to put it into your mysite/_config.php


13 February 2011

Comments (frozen for the time being)

  • Hello,
    I am working in a site and need to put the GA code, but I can't succed. I've tried to follow your instructions but I am not so good with coding and can't manage to do it... I have acces to de "admin" page from SilverStripe of the website, where I can add pages, content and so on, but can't add the code. Could you help me please?

    Posted by Ester, 23/02/2013 12:09pm (7 years ago)


Интернет реклама