]> _ Git - cubist_gtag.git/commitdiff
wip #2609 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 22 Feb 2019 17:19:17 +0000 (18:19 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 22 Feb 2019 17:19:17 +0000 (18:19 +0100)
.editorconfig [new file with mode: 0644]
composer.json
resources/config/config.php [new file with mode: 0644]
resources/views/script.blade.php [new file with mode: 0644]
src/Cubist/Gtag/SayHello.php [deleted file]
src/GoogleAnalytics.php [new file with mode: 0644]
src/GtagFrontProvider.php [new file with mode: 0644]
src/ScriptViewCreator.php [new file with mode: 0644]

diff --git a/.editorconfig b/.editorconfig
new file mode 100644 (file)
index 0000000..4d1d98f
--- /dev/null
@@ -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
index 88eef93155d2c99da5eedb3740713c7d802d9740..da7d08856d8599e4c7ef73a9f8a27f2a00ecd63d 100644 (file)
@@ -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 (file)
index 0000000..3b2bff4
--- /dev/null
@@ -0,0 +1,14 @@
+<?php
+
+return [
+
+    /*
+     * The Google Tag Manager id, should be a code that looks something like "gtm-xxxx".
+     */
+    'id' => 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 (file)
index 0000000..f2e5548
--- /dev/null
@@ -0,0 +1,14 @@
+@if($enabled)
+    {{--<!-- Global site tag (gtag.js) - Google Analytics -->--}}
+    <script async src="https://www.googletagmanager.com/gtag/js?id={{$id}}"></script>
+    <script>
+        window.dataLayer = window.dataLayer || [];
+
+        function gtag() {
+            dataLayer.push(arguments);
+        }
+
+        gtag('js', new Date());
+        gtag('config', '{{$id}}');
+    </script>
+@endif
diff --git a/src/Cubist/Gtag/SayHello.php b/src/Cubist/Gtag/SayHello.php
deleted file mode 100644 (file)
index 95e7081..0000000
+++ /dev/null
@@ -1,9 +0,0 @@
-<?php
-namespace Cubist\Gtag;
-class SayHello
-{
-        public static function world()
-        {
-                return 'Hello World, Composer!';
-        }
-}
\ No newline at end of file
diff --git a/src/GoogleAnalytics.php b/src/GoogleAnalytics.php
new file mode 100644 (file)
index 0000000..2b087ad
--- /dev/null
@@ -0,0 +1,63 @@
+<?php
+
+namespace Cubist\GoogleAnalytics;
+
+
+class GoogleAnalytics
+{
+
+    /**
+     * @var string
+     */
+    protected $id;
+
+    /**
+     * @var bool
+     */
+    protected $enabled;
+
+    /**
+     * @param string $id
+     */
+    public function __construct($id)
+    {
+        $this->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 (file)
index 0000000..2bf8d5f
--- /dev/null
@@ -0,0 +1,49 @@
+<?php
+
+namespace Cubist\Gtag;
+
+use Illuminate\Support\ServiceProvider;
+
+
+class GtagServiceProvider extends ServiceProvider
+{
+    /**
+     * Perform post-registration booting of services.
+     *
+     * @return void
+     */
+    public function boot()
+    {
+        $this->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 (file)
index 0000000..18885f3
--- /dev/null
@@ -0,0 +1,36 @@
+<?php
+
+namespace Cubist\Gtag;
+
+use Cubist\GoogleAnalytics\GoogleAnalytics;
+use Illuminate\View\View;
+
+class ScriptViewCreator
+{
+       /**
+        * @var GoogleAnalytics
+        */
+       protected $googleAnalytics;
+
+
+       public function __construct(GoogleAnalytics $googleAnalytics)
+       {
+               $this->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());
+       }
+}