-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1f43db3
Showing
6 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Bigfork Google Analytics | ||
|
||
Slimline module for configuring Google Analytics via SilverStripe CMS. Designed for use by [Bigfork]('http://www.bigfork.co.uk') - feel free to use/modify it. | ||
|
||
##Installation: | ||
```composer require bigfork/bfgoogleanalytics ^3.0``` | ||
|
||
Add tracking code details via "Settings" menu. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
Name: 'google-analytics' | ||
--- | ||
SilverStripe\SiteConfig\SiteConfig: | ||
extensions: | ||
- BigforkGoogleAnalytics\GoogleConfig | ||
SilverStripe\CMS\Controllers\ContentController: | ||
extensions: | ||
- BigforkGoogleAnalytics\GoogleLogger |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace BigforkGoogleAnalytics; | ||
|
||
use SilverStripe\Forms\FieldList; | ||
use SilverStripe\Forms\LiteralField; | ||
use SilverStripe\Forms\Tab; | ||
use SilverStripe\Forms\TextField; | ||
use SilverStripe\Forms\TextareaField; | ||
use SilverStripe\ORM\DataExtension; | ||
|
||
class GoogleConfig extends DataExtension | ||
{ | ||
private static $db = [ | ||
'GoogleAnalyticsTrackingID' => 'Varchar', | ||
'GoogleAnalyticsParameters' => 'Text', | ||
'GoogleAnalyticsConstructorParameters' => 'Text' | ||
]; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function updateCMSFields(FieldList $fields) | ||
{ | ||
$analyticsFields = FieldList::create( | ||
LiteralField::create('', '<div class="message warning">Tracking will be output using gtag.js</div>'), | ||
TextField::create("GoogleAnalyticsTrackingID", "Google Analytics Tracking ID") | ||
->setDescription("e.g. UA-XXXXXX-X"), | ||
TextareaField::create("GoogleAnalyticsConstructorParameters", "Additional Configuration") | ||
->setDescription("<strong>Advanced users only.</strong> | ||
An object to be passed as an argument to gtag config. If you do not know what this field does, please leave it blank. "), | ||
TextareaField::create("GoogleAnalyticsParameters", "Additional Google Analytics Properties") | ||
->setDescription("<strong>Advanced users only.</strong> | ||
Insert additional code after the initial gtag config. If you do not know what this does, please leave this field blank.") | ||
); | ||
|
||
$fields->addFieldToTab('Root', Tab::create('GoogleAnalytics')->setChildren($analyticsFields)); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace BigforkGoogleAnalytics; | ||
|
||
use SilverStripe\Core\Extension; | ||
use SilverStripe\SiteConfig\SiteConfig; | ||
use SilverStripe\View\ArrayData; | ||
use SilverStripe\View\Requirements; | ||
|
||
class GoogleLogger extends Extension | ||
{ | ||
public function onAfterInit() | ||
{ | ||
if ($this->owner->getRequest()->getVar('CMSPreview')) { | ||
return; | ||
} | ||
|
||
$config = SiteConfig::current_site_config(); | ||
|
||
// include the JS snippet into the frontend page markup | ||
if ($trackingID = $config->GoogleAnalyticsTrackingID) { | ||
$analyticsData = new ArrayData([ | ||
'GoogleAnalyticsTrackingID' => $trackingID, | ||
'GoogleAnalyticsParameters' => $config->GoogleAnalyticsParameters, | ||
'GoogleAnalyticsConstructorParameters' => $config->GoogleAnalyticsConstructorParameters | ||
]); | ||
Requirements::insertHeadTags($analyticsData->renderWith('GoogleAnalyticsJSSnippet'), 'GoogleAnalytics'); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "bigfork/google-analytics", | ||
"type": "silverstripe-vendormodule", | ||
"description": "Slimline module for configuring Google Analytics via SilverStripe CMS. Intended for advanced users, the ability to add parameters through CMS makes it very possible to break the whole site.", | ||
"authors": [{ | ||
"name": "Loz Calver", | ||
"email": "lozcalver@bigfork.co.uk" | ||
}, { | ||
"name": "Colin Richardson", | ||
"email": "colinrichardson@bigfork.co.uk" | ||
}], | ||
"require": | ||
{ | ||
"silverstripe/framework": "^4", | ||
"silverstripe/cms": "^4" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<script async src="https://www.googletagmanager.com/gtag/js?id={$GoogleAnalyticsTrackingID}"></script> | ||
<script> | ||
window.dataLayer = window.dataLayer || []; | ||
function gtag(){dataLayer.push(arguments);} | ||
gtag('js', new Date()); | ||
gtag('config', '{$GoogleAnalyticsTrackingID}'<% if $GoogleAnalyticsConstructorParameters %>, {$GoogleAnalyticsConstructorParameters.RAW}<% end_if %>);<% if $GoogleAnalyticsParameters %> | ||
{$GoogleAnalyticsParameters.RAW}<% end_if %> | ||
</script> |