From 1f43db3a0c2446fb886b855134634a77d296c95e Mon Sep 17 00:00:00 2001 From: Colin Richardson Date: Wed, 22 May 2019 16:04:24 +0100 Subject: [PATCH] initial commmit --- README.md | 8 ++++++ _config/config.yml | 9 +++++++ code/GoogleConfig.php | 39 +++++++++++++++++++++++++++ code/GoogleLogger.php | 30 +++++++++++++++++++++ composer.json | 17 ++++++++++++ templates/GoogleAnalyticsJSSnippet.ss | 8 ++++++ 6 files changed, 111 insertions(+) create mode 100644 README.md create mode 100644 _config/config.yml create mode 100644 code/GoogleConfig.php create mode 100644 code/GoogleLogger.php create mode 100644 composer.json create mode 100644 templates/GoogleAnalyticsJSSnippet.ss diff --git a/README.md b/README.md new file mode 100644 index 0000000..59e089e --- /dev/null +++ b/README.md @@ -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. diff --git a/_config/config.yml b/_config/config.yml new file mode 100644 index 0000000..6b7e7b5 --- /dev/null +++ b/_config/config.yml @@ -0,0 +1,9 @@ +--- +Name: 'google-analytics' +--- +SilverStripe\SiteConfig\SiteConfig: + extensions: + - BigforkGoogleAnalytics\GoogleConfig +SilverStripe\CMS\Controllers\ContentController: + extensions: + - BigforkGoogleAnalytics\GoogleLogger diff --git a/code/GoogleConfig.php b/code/GoogleConfig.php new file mode 100644 index 0000000..f97c67a --- /dev/null +++ b/code/GoogleConfig.php @@ -0,0 +1,39 @@ + 'Varchar', + 'GoogleAnalyticsParameters' => 'Text', + 'GoogleAnalyticsConstructorParameters' => 'Text' + ]; + + /** + * @inheritdoc + */ + public function updateCMSFields(FieldList $fields) + { + $analyticsFields = FieldList::create( + LiteralField::create('', '
Tracking will be output using gtag.js
'), + TextField::create("GoogleAnalyticsTrackingID", "Google Analytics Tracking ID") + ->setDescription("e.g. UA-XXXXXX-X"), + TextareaField::create("GoogleAnalyticsConstructorParameters", "Additional Configuration") + ->setDescription("Advanced users only. + 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("Advanced users only. + 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)); + } +} diff --git a/code/GoogleLogger.php b/code/GoogleLogger.php new file mode 100644 index 0000000..d4cf7a0 --- /dev/null +++ b/code/GoogleLogger.php @@ -0,0 +1,30 @@ +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'); + } + } +} diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..2f7b18b --- /dev/null +++ b/composer.json @@ -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" + } +} diff --git a/templates/GoogleAnalyticsJSSnippet.ss b/templates/GoogleAnalyticsJSSnippet.ss new file mode 100644 index 0000000..c7d295d --- /dev/null +++ b/templates/GoogleAnalyticsJSSnippet.ss @@ -0,0 +1,8 @@ + +