Google Universal Analytics.js tracking code for SilverStripe
An extension to add Google Universal Analytics.js tracking code (
ga()) to your SilverStripe templates.
It automatically includes optional unobtrustive Google Analytics event tracking for all outbound links & "asset downloads", as well as event tracking for 404 and error pages, email & telephone links.
- Google Universal Analytics
Analytics.jscode injected automatically into
ga()functions in your other code), as per GA recommendation.
pageviewtracking for all configured accounts, including tracking of 404 & 500 page errors (tracked as "Page Not Found" / "Page Error" events).
- Unobtrustive oubound, download, email & tel: link tracking - monitors all page clicks, rather than on page load (ie: works with links including those generated by Ajax etc on the page after page load).
- Uses Google Analytics
hitCallbackfor tracking for outgoing links to register before load (ie: when no link
targetis set) to ensure tracker is successfully run before redirection.
- Tracking codes are automatically changed to
SS_ENVIRONMENT_TYPEis not live, or if page URL matches ?flush= to prevent bad data capture.
- Optionally use a locally cached
analytics.jsinstead of the Google-hosted version. For more information please read docs/en/Caching.md.
Additional event tracking is automatically enabled by default for:
- File downloads (all non-image files in the assets folder) are tracked as "Downloads".
- Outgoing links are tracked as "Outgoing Links".
- Email (
mailto:) links are tracked as "Email Links".
- Phone (
tel:) links are tracked as "Phone Links".
Event category names (eg: "Outgoing Links", "Downloads" etc) can be configured in your yaml config.
Note: Event tracking only works with regular (left-or-middle) mouse button clicks (including combinations with Ctrl/Shift/Meta keys). Tracking is bypassed if the user right-clicks on a link and selects an action from the context menu (open in new tab, save as etc...). Unfortunately there is no way around this without disabeling the content menu entirely.
- SilverStripe 4.*
- Guzzle (if local
analytics.jscaching is enabled)
For SilverStripe 3, please refer to the SilverStripe3 branch.
You can install it via composer with
composer require axllent/silverstripe-analytics-js
If you require local caching of Google Analytics, then you must have Guzzle installed too.
composer require guzzlehttp/guzzle
Once installed the extension is automatically loaded if you provide at least one tracking account in your config yaml file (eg)
Axllent\AnalyticsJS\AnalyticsJS: tracker: - ['create', 'UA-1234567-1', 'auto']
The syntax is very similar to the official documentation, so things like secondary trackers or other configurations can be easily added. Please note that secondary trackers must contain a unique
Please refer to the Configuration.md for all configuration options.
To start live tracking, make sure your website is in
live mode, or if running in
staging that you
track_in_dev_mode: true option.