Created
April 7, 2016 05:25
-
-
Save camfindlay/0903b35dbd5cbc98999a8f4b719fde6d to your computer and use it in GitHub Desktop.
Example of custom Google Analytics integration for SilverStripe CMS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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')); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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