From 568070da01b1cb785295b49f8a8dfead681b4e30 Mon Sep 17 00:00:00 2001 From: Michal Kleiner Date: Mon, 6 Feb 2017 23:55:21 +1300 Subject: [PATCH] Init the module - version 1.0.0 --- .editorconfig | 17 +++++++ .gitattributes | 6 +++ CHANGELOG.md | 16 ++++++ LICENSE.md | 26 ++++++++++ README.md | 62 +++++++++++++++++++++++ _config.php | 1 + _config/config.yml | 9 ++++ code-of-conduct.md | 1 + code/extensions/GASiteConfigExtension.php | 42 +++++++++++++++ code/formfields/GACodeField.php | 43 ++++++++++++++++ composer.json | 19 +++++++ templates/GA_GTM_body.ss | 10 ++++ templates/GA_GTM_head.ss | 10 ++++ templates/includes/GA_body.ss | 0 templates/includes/GA_head.ss | 11 ++++ templates/includes/GTM_body.ss | 3 ++ templates/includes/GTM_head.ss | 7 +++ 17 files changed, 283 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 CHANGELOG.md create mode 100644 LICENSE.md create mode 100644 README.md create mode 100644 _config.php create mode 100644 _config/config.yml create mode 100644 code-of-conduct.md create mode 100644 code/extensions/GASiteConfigExtension.php create mode 100644 code/formfields/GACodeField.php create mode 100644 composer.json create mode 100644 templates/GA_GTM_body.ss create mode 100644 templates/GA_GTM_head.ss create mode 100644 templates/includes/GA_body.ss create mode 100644 templates/includes/GA_head.ss create mode 100644 templates/includes/GTM_body.ss create mode 100644 templates/includes/GTM_head.ss diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..47ae637 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,17 @@ +# For more information about the properties used in this file, +# please see the EditorConfig documentation: +# http://editorconfig.org + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 4 +indent_style = space +insert_final_newline = true +trim_trailing_whitespace = true + +[{*.yml,package.json}] +indent_size = 2 + +# The indent size used in the package.json file cannot be changed: +# https://github.com/npm/npm/pull/3180#issuecomment-16336516 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..475f5f2 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,6 @@ +/tests export-ignore +/docs export-ignore +/.gitattributes export-ignore +/.gitignore export-ignore +/.travis.yml export-ignore +/.scrutinizer.yml export-ignore diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..03f171a --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,16 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +This project adheres to [Semantic Versioning](http://semver.org/). + +## [1.0.0] + +Initial module release. + +### Bugfixes + + +### API + + diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..e78cbee --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,26 @@ +Copyright (c) 2017, Chrometoaster New Media Ltd. +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. Neither the name of nor the names of its contributors may be used to + endorse or promote products derived from this software without specific + prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..789b5fa --- /dev/null +++ b/README.md @@ -0,0 +1,62 @@ +# GA/GTM single-field + +Adds support for a single-field configuration of GA/GTM code within the CMS. +Comes with Google snippets that need to be included in your theme's templates. + +## Requirements + +* SilverStripe 3.1 and above + +## Installation + +### Module + +```sh +$ composer require chrometoaster/ga-gtm-singlefield +``` + +Alternatively, you can also install the module manually by placing a copy to the root folder of your site. + +Run dev/build either via opening the url `http:///dev/build` in a browser or +by running the dev/build via a CLI. + +### Theme integration + +Place two template includes into your theme's main template, e.g. Page.ss. + +#### Head + +`<% include GA_GTM_head %>` as close to the opening `` tag as possible: + +```html + + + $Title + + <% include GA_GTM_head %> +``` + +#### Body + +`<% include GA_GTM_body %>` directly after the opening `` tag: + +```html + + + <% include GA_GTM_body %> +``` + + +## Usage + +You will see a new 'GA & GTM' tab with a single field for a GA or GTM code under Settings in the CMS. +Insert a valid GA or GTM code and check if it's getting output to the resulting markup correctly. + +## Changelog + +For details of updates, bugfixes, and features, please see the [changelog](CHANGELOG.md). + +## TODO + +* Replace template includes with TemplateGlobalProvider or Controller function. +* Investigate options for automatically inserting the necessary tags to the markup, e.g. extending the $MetaTags functionality or using Requirements backend. diff --git a/_config.php b/_config.php new file mode 100644 index 0000000..b3d9bbc --- /dev/null +++ b/_config.php @@ -0,0 +1 @@ + 'Varchar(5)', + 'GoogleCode' => 'Varchar(15)', + ]; + + public function updateCMSFields(FieldList $fields) + { + $fields->findOrMakeTab('Root.GAGTM', 'GA & GTM'); + + $fields->addFieldsToTab('Root.GAGTM', [ + GACodeField::create('GoogleCode', 'Google code')->setDescription('Can be either a Universal (UA-XXXXXXXX-X) or Tag manager (GTM-XXXXXX) code.'), + ]); + } + + /** + * Update the AnalyticType field with a key depending on what type of code is used. + */ + public function onBeforeWrite() + { + $parts = explode("-", $this->owner->getField('GoogleCode')); + + if ($parts[0] === "GTM") { + $this->owner->setField('AnalyticType', 'GTM'); + + } else if ($parts[0] === "UA") { + $this->owner->setField('AnalyticType', 'UA'); + } + + parent::onBeforeWrite(); + } + +} diff --git a/code/formfields/GACodeField.php b/code/formfields/GACodeField.php new file mode 100644 index 0000000..b937ea8 --- /dev/null +++ b/code/formfields/GACodeField.php @@ -0,0 +1,43 @@ +value)) { + return true; + } else { + $parts = explode("-", $this->value); + } + + if ($parts[0] === "GTM" && (mb_strlen($parts[1]) >= 4) && (mb_strlen($parts[1]) <= 8)) { + return true; + + } else if (($parts[0] === "UA") && mb_strlen($parts[1]) === 8 && mb_strlen($parts[2]) === 1) { + return true; + + } else { + $validator->validationError($this->name, "ERROR: This isn't a valid Universal or Tag Manager code. Must be in either UA-XXXXXXXX-X or GTM-XXXXXX format.", "validation"); + } + + return false; + } +} diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..ed8e5cc --- /dev/null +++ b/composer.json @@ -0,0 +1,19 @@ +{ + "name": "chrometoaster/ga-gtm-singlefield", + "description": "Adds single field to CMS settings section to hold GA or GTM code. Template snippets need to be inserted to theme templates manually.", + "type": "silverstripe-module", + "keywords": ["silverstripe", "Google", "Google Analytics", "GA", "Google Tag Manager", "GTM"], + "authors": [ + { + "name": "Michal Kleiner", + "email": "michal.kleiner@chrometoaster.com" + } + ], + "require": { + "php": ">=5.4", + "silverstripe/framework": "~3.1", + "silverstripe/cms": "~3.1" + }, + "minimum-stability": "stable", + "license": "BSD-3-Clause" +} diff --git a/templates/GA_GTM_body.ss b/templates/GA_GTM_body.ss new file mode 100644 index 0000000..acd5edf --- /dev/null +++ b/templates/GA_GTM_body.ss @@ -0,0 +1,10 @@ +<% with $SiteConfig %> + <% if $GoogleCode %> + <% if $AnalyticType == 'GTM' %> + <% include GTM_body %> + <% end_if %> + <% if $AnalyticType == 'UA' %> + <% include GA_body %> + <% end_if %> + <% end_if %> +<% end_with %> diff --git a/templates/GA_GTM_head.ss b/templates/GA_GTM_head.ss new file mode 100644 index 0000000..7191b96 --- /dev/null +++ b/templates/GA_GTM_head.ss @@ -0,0 +1,10 @@ +<% with $SiteConfig %> + <% if $GoogleCode %> + <% if $AnalyticType == 'GTM' %> + <% include GTM_head %> + <% end_if %> + <% if $AnalyticType == 'UA' %> + <% include GA_head %> + <% end_if %> + <% end_if %> +<% end_with %> diff --git a/templates/includes/GA_body.ss b/templates/includes/GA_body.ss new file mode 100644 index 0000000..e69de29 diff --git a/templates/includes/GA_head.ss b/templates/includes/GA_head.ss new file mode 100644 index 0000000..a2385f5 --- /dev/null +++ b/templates/includes/GA_head.ss @@ -0,0 +1,11 @@ + + + diff --git a/templates/includes/GTM_body.ss b/templates/includes/GTM_body.ss new file mode 100644 index 0000000..fd2c49a --- /dev/null +++ b/templates/includes/GTM_body.ss @@ -0,0 +1,3 @@ + + + diff --git a/templates/includes/GTM_head.ss b/templates/includes/GTM_head.ss new file mode 100644 index 0000000..cfc0c01 --- /dev/null +++ b/templates/includes/GTM_head.ss @@ -0,0 +1,7 @@ + + +