Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of custom Google Analytics integration for SilverStripe CMS
---
Name: mysite
After:
- 'framework/*'
- 'cms/*'
---
# YAML configuration for SilverStripe
# See http://doc.silverstripe.org/framework/en/topics/configuration
# Caution: Indentation through two spaces, not tabs
SSViewer:
theme: 'simple'
SiteConfig:
extensions:
- GoogleAnalyticsExtension
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', '$trackingid', 'auto');
ga('send', 'pageview');
//Event tracking or any other advanced GA code
$('a#important-download').click(function() {
ga('send', 'event', 'important-ebook', 'download', 'Marketing Campaign');
});
<?php
class GoogleAnalyticsExtension extends DataExtension {
private static $db = array(
'GoogleAnalyticsTrackingID' => 'Varchar(11)',
);
public function updateCMSFields(FieldList $fields)
{
$fields->addFieldToTab('Root.GoogleAnalytics', TextField::create('GoogleAnalyticsTrackingID')->setDescription('e.g. UA-XXXXX-XX'));
}
}
<?php
class Page extends SiteTree {
}
class Page_Controller extends ContentController {
public function init() {
parent::init();
Requirements::javascriptTemplate(
'mysite/javascript/google-analytics.js',
array(
'trackingid' => SiteConfig::current_site_config()->GoogleAnalyticsTrackingID,
)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.