From 24d6a49a3378c2b5af987a1bd61de72cb18cf377 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Fri, 22 Feb 2019 18:19:17 +0100 Subject: [PATCH] wip #2609 @1 --- .editorconfig | 15 ++++++++ composer.json | 41 ++++++++++++++++++++- resources/config/config.php | 14 +++++++ resources/views/script.blade.php | 14 +++++++ src/Cubist/Gtag/SayHello.php | 9 ----- src/GoogleAnalytics.php | 63 ++++++++++++++++++++++++++++++++ src/GtagFrontProvider.php | 49 +++++++++++++++++++++++++ src/ScriptViewCreator.php | 36 ++++++++++++++++++ 8 files changed, 231 insertions(+), 10 deletions(-) create mode 100644 .editorconfig create mode 100644 resources/config/config.php create mode 100644 resources/views/script.blade.php delete mode 100644 src/Cubist/Gtag/SayHello.php create mode 100644 src/GoogleAnalytics.php create mode 100644 src/GtagFrontProvider.php create mode 100644 src/ScriptViewCreator.php diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..4d1d98f --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +; This file is for unifying the coding style for different editors and IDEs. +; More information at http://editorconfig.org + +root = true + +[*] +charset = utf-8 +indent_size = 4 +indent_style = space +end_of_line = lf +insert_final_newline = false +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false diff --git a/composer.json b/composer.json index 88eef93..da7d088 100644 --- a/composer.json +++ b/composer.json @@ -1 +1,40 @@ -{"name":"cubist\/gtag","description":"gtag cubist composer package","type":"library","license":"proprietary","minimum-stability":"dev","autoload":{"psr-0":{"Cubist\\Gtag":"src\/"}},"authors":[{"name":"Vincent Vanwaelscappel","email":"vincent@cubedesigners.com"}],"require":{"php":">=5.4.0"},"repositories":[{"type":"composer","url":"https:\/\/composer.cubedesigners.com\/"}]} \ No newline at end of file +{ + "name": "cubist\/gtag", + "description": "Google analytics tracking code for Laravel", + "type": "library", + "license": "proprietary", + "minimum-stability": "dev", + "keywords": [ + "cubist", + "gtag", + "google analytics" + ], + "autoload": { + "psr-4": { + "Cubist\\Gtag": "src" + } + }, + "authors": [ + { + "name": "Vincent Vanwaelscappel", + "email": "vincent@cubedesigners.com" + } + ], + "require": { + "php": ">=5.5.0", + "laravel/framework": "5.7.*" + }, + "repositories": [ + { + "type": "composer", + "url": "https:\/\/composer.cubedesigners.com\/" + } + ], + "extra": { + "laravel": { + "providers": [ + "Cubist\\Gtag\\GtagServiceProvider" + ] + } + } +} diff --git a/resources/config/config.php b/resources/config/config.php new file mode 100644 index 0000000..3b2bff4 --- /dev/null +++ b/resources/config/config.php @@ -0,0 +1,14 @@ + env('GOOGLE_ANALYTICS_ID', ''), + + /* + * Enable or disable script rendering. Useful for local development. + */ + 'enabled' => env('GOOGLE_ANALYTICS_ID', '') != '', +]; diff --git a/resources/views/script.blade.php b/resources/views/script.blade.php new file mode 100644 index 0000000..f2e5548 --- /dev/null +++ b/resources/views/script.blade.php @@ -0,0 +1,14 @@ +@if($enabled) + {{----}} + + +@endif diff --git a/src/Cubist/Gtag/SayHello.php b/src/Cubist/Gtag/SayHello.php deleted file mode 100644 index 95e7081..0000000 --- a/src/Cubist/Gtag/SayHello.php +++ /dev/null @@ -1,9 +0,0 @@ -id = $id; + $this->enabled = true; + } + + /** + * Return the Google Analytics id. + * + * @return string + */ + public function id() + { + return $this->id; + } + + /** + * Check whether script rendering is enabled. + * + * @return bool + */ + public function isEnabled() + { + return $this->enabled; + } + + /** + * Enable Google Analytics scripts rendering. + */ + public function enable() + { + $this->enabled = true; + } + + /** + * Disable Google Analytics scripts rendering. + */ + public function disable() + { + $this->enabled = false; + } +} diff --git a/src/GtagFrontProvider.php b/src/GtagFrontProvider.php new file mode 100644 index 0000000..2bf8d5f --- /dev/null +++ b/src/GtagFrontProvider.php @@ -0,0 +1,49 @@ +loadViewsFrom(__DIR__.'/../../resources/views', 'gtag'); + + $this->publishes([ + __DIR__.'/../../resources/config/config.php' => config_path('googleanalytics.php'), + ], 'config'); + + $this->publishes([ + __DIR__.'/../../resources/views' => base_path('resources/views/vendor/cubist/gtag'), + ], 'views'); + + $this->app['view']->creator( + ['gtag::script'], + 'Cubist\Gtag\ScriptViewCreator' + ); + } + + /** + * Register the application services. + */ + public function register() + { + $this->mergeConfigFrom(__DIR__ . '/../../resources/config/config.php', 'gtag'); + + $googleAnalytics = new GoogleAnalytics(config('gtag.id')); + + if (config('gtag.enabled') === false) { + $googleAnalytics->disable(); + } + + $this->app->instance('Cubist\Gtag\GoogleAnalytics', $googleAnalytics); + $this->app->alias('Cubist\Gtag\GoogleAnalytics', 'gtag'); + } +} \ No newline at end of file diff --git a/src/ScriptViewCreator.php b/src/ScriptViewCreator.php new file mode 100644 index 0000000..18885f3 --- /dev/null +++ b/src/ScriptViewCreator.php @@ -0,0 +1,36 @@ +googleAnalytics = $googleAnalytics; + } + + /** + * Bind data to the view. + * + * @param View $view + */ + public function create(View $view) + { + if ($this->googleAnalytics->isEnabled() && empty($this->googleAnalytics->id())) { + return; + } + + $view + ->with('enabled', $this->googleAnalytics->isEnabled()) + ->with('id', $this->googleAnalytics->id()); + } +} -- 2.39.5