--- /dev/null
+; 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
-{"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"
+ ]
+ }
+ }
+}
--- /dev/null
+<?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', '') != '',
+];
--- /dev/null
+@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
+++ /dev/null
-<?php
-namespace Cubist\Gtag;
-class SayHello
-{
- public static function world()
- {
- return 'Hello World, Composer!';
- }
-}
\ No newline at end of file
--- /dev/null
+<?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;
+ }
+}
--- /dev/null
+<?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
--- /dev/null
+<?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());
+ }
+}