diff --git a/README.md b/README.md index 4472a40..a6d0504 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ SEO Helper is a package that provides tools and helpers for SEO (Search Engine O * Framework agnostic package. * Open Graph & Twitter Cards are supported. * Webmaster tools site verifier tags are supported. + * Google Analytics tracking is supported. * Easy setup & configuration. * Well documented & IDE Friendly. * Well tested with maximum code quality. @@ -52,7 +53,6 @@ SEO Helper is a package that provides tools and helpers for SEO (Search Engine O ## TODOS - [ ] Complete the documentation. - - [ ] Adding Google Analytics support. ## DONE @@ -60,6 +60,7 @@ SEO Helper is a package that provides tools and helpers for SEO (Search Engine O - [x] Open Graph supported. - [x] Twitter Cards supported. - [x] Webmaster tools supported. + - [x] Google Analytics supported. - [x] Laravel 5.1 supported. ## Contribution diff --git a/_docs/4-Usage.md b/_docs/4-Usage.md index 4261255..e06a862 100644 --- a/_docs/4-Usage.md +++ b/_docs/4-Usage.md @@ -10,6 +10,7 @@ * [Keywords](#keywords) * [Miscellaneous Tags](#miscellaneous-tags) * [Webmasters](#webmasters) + * [Analytics](#analytics) * [Open Graph](#open-graph) * [Twitter Card](#twitter-card) 2. [Helpers](#2-helpers) @@ -703,6 +704,13 @@ echo $webmasters->render(); For more details, check the [Webmasters API](5-API.md#webmasters). +### Analytics + +```php +``` + +For more details, check the [Webmasters API](5-API.md#analytics). + ### Open Graph It's pretty darn easy, right ? for the open graph protocol, same old stuff : diff --git a/_docs/5-API.md b/_docs/5-API.md index fd1a0db..16328a2 100644 --- a/_docs/5-API.md +++ b/_docs/5-API.md @@ -1,4 +1,4 @@ -# 5. API +# 5. API ## Table of contents @@ -10,6 +10,7 @@ * [Keywords](#keywords) * [Miscellaneous Tags](#miscellaneous-tags) * [Webmasters](#webmasters) + * [Analytics](#analytics) * [Open Graph](#open-graph) * [Twitter Card](#twitter-card) 3. [Helpers](#3-helpers) @@ -18,7 +19,7 @@ * [SEO Meta](#seo-meta) * [SEO Open Graph](#seo-open-graph) * [SEO Twitter Card](#seo-twitter-card) - + ## 1. Contracts ### Renderable @@ -216,7 +217,7 @@ interface DescriptionInterface extends Renderable * @return self */ public function setMax($max); - + /* ------------------------------------------------------------------------------------------------ | Main Functions | ------------------------------------------------------------------------------------------------ @@ -407,6 +408,30 @@ interface WebmastersInterface extends Renderable } ``` +### Analytics + +```php + [ + 'google' => '', // UA-XXXXXXXX-X + ], ]; diff --git a/src/Contracts/Entities/AnalyticsInterface.php b/src/Contracts/Entities/AnalyticsInterface.php new file mode 100644 index 0000000..ff40498 --- /dev/null +++ b/src/Contracts/Entities/AnalyticsInterface.php @@ -0,0 +1,25 @@ + + */ +interface AnalyticsInterface extends Renderable +{ + /* ------------------------------------------------------------------------------------------------ + | Getters & Setters + | ------------------------------------------------------------------------------------------------ + */ + /** + * Set google analytics code. + * + * @param string $code + * + * @return self + */ + public function setGoogle($code); +} diff --git a/src/Contracts/SeoMeta.php b/src/Contracts/SeoMeta.php index 82921a3..bbf435a 100644 --- a/src/Contracts/SeoMeta.php +++ b/src/Contracts/SeoMeta.php @@ -123,6 +123,15 @@ public function addWebmaster($webmaster, $content); */ public function setUrl($url); + /** + * Set the google analytics code. + * + * @param string $code + * + * @return self + */ + public function setGoogleAnalytics($code); + /* ------------------------------------------------------------------------------------------------ | Main Functions | ------------------------------------------------------------------------------------------------ diff --git a/src/Entities/Analytics.php b/src/Entities/Analytics.php new file mode 100644 index 0000000..67880a0 --- /dev/null +++ b/src/Entities/Analytics.php @@ -0,0 +1,113 @@ + + */ +class Analytics implements AnalyticsInterface +{ + /* ------------------------------------------------------------------------------------------------ + | Traits + | ------------------------------------------------------------------------------------------------ + */ + use Configurable; + + /* ------------------------------------------------------------------------------------------------ + | Properties + | ------------------------------------------------------------------------------------------------ + */ + /** + * Google analytics code. + * + * @var string + */ + protected $google = ''; + + /* ------------------------------------------------------------------------------------------------ + | Constructor + | ------------------------------------------------------------------------------------------------ + */ + /** + * Make an Analytics instance. + * + * @param array $config + */ + public function __construct(array $config) + { + $this->setConfigs($config); + + $this->setGoogle($this->getConfig('google', '')); + } + + /* ------------------------------------------------------------------------------------------------ + | Getters & Setters + | ------------------------------------------------------------------------------------------------ + */ + /** + * Set google analytics code. + * + * @param string $code + * + * @return self + */ + public function setGoogle($code) + { + $this->google = $code; + + return $this; + } + + /* ------------------------------------------------------------------------------------------------ + | Main Functions + | ------------------------------------------------------------------------------------------------ + */ + /** + * Render the tag. + * + * @return string + */ + public function render() + { + return implode(PHP_EOL, array_filter([ + $this->renderGoogleScript(), + ])); + } + + /** + * Render the tag. + * + * @return string + */ + public function __toString() + { + return $this->render(); + } + + /* ------------------------------------------------------------------------------------------------ + | Other Functions + | ------------------------------------------------------------------------------------------------ + */ + /** + * Render the google tracking code. + * + * @return string + */ + private function renderGoogleScript() + { + return << + (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ + (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), + m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) + })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); + ga('create', '$this->google', 'auto'); + ga('send', 'pageview'); + +EOT; + } +} diff --git a/src/SeoMeta.php b/src/SeoMeta.php index cc409c0..76c33b5 100644 --- a/src/SeoMeta.php +++ b/src/SeoMeta.php @@ -1,5 +1,6 @@ webmasters( new Entities\Webmasters($this->getConfig('webmasters', [])) ); + $this->analytics( + new Entities\Analytics($this->getConfig('analytics', [])) + ); } /* ------------------------------------------------------------------------------------------------ @@ -178,6 +189,20 @@ public function webmasters(WebmastersInterface $webmasters) return $this; } + /** + * Set the Analytics instance. + * + * @param Contracts\Entities\AnalyticsInterface $analytics + * + * @return self + */ + private function analytics(AnalyticsInterface $analytics) + { + $this->analytics = $analytics; + + return $this; + } + /** * Set the title. * @@ -288,6 +313,20 @@ public function setUrl($url) return $this; } + /** + * Set the Google Analytics code. + * + * @param string $code + * + * @return self + */ + public function setGoogleAnalytics($code) + { + $this->analytics->setGoogle($code); + + return $this; + } + /* ------------------------------------------------------------------------------------------------ | Main Functions | ------------------------------------------------------------------------------------------------ @@ -372,6 +411,7 @@ public function render() $this->keywords->render(), $this->misc->render(), $this->webmasters->render(), + $this->analytics->render(), ])); } diff --git a/tests/Entities/AnalyticsTest.php b/tests/Entities/AnalyticsTest.php new file mode 100644 index 0000000..38db270 --- /dev/null +++ b/tests/Entities/AnalyticsTest.php @@ -0,0 +1,71 @@ + + */ +class AnalyticsTest extends TestCase +{ + /* ------------------------------------------------------------------------------------------------ + | Properties + | ------------------------------------------------------------------------------------------------ + */ + /** @var Analytics */ + private $analytics; + + /* ------------------------------------------------------------------------------------------------ + | Main Functions + | ------------------------------------------------------------------------------------------------ + */ + public function setUp() + { + parent::setUp(); + + $config = $this->getSeoHelperConfig('analytics', []); + $this->analytics = new Analytics($config); + } + + public function tearDown() + { + parent::tearDown(); + + unset($this->analytics); + } + + /* ------------------------------------------------------------------------------------------------ + | Test Functions + | ------------------------------------------------------------------------------------------------ + */ + /** @test */ + public function it_can_be_instantiated() + { + $expectations = [ + \Arcanedev\SeoHelper\Entities\Analytics::class, + \Arcanedev\SeoHelper\Contracts\Renderable::class, + ]; + + foreach ($expectations as $expected) { + $this->assertInstanceOf($expected, $this->analytics); + } + } + + /** @test */ + public function it_can_render() + { + $expectations = [ + '', + ]; + + foreach ($expectations as $expected) { + $this->assertContains($expected, $this->analytics->render()); + $this->assertContains($expected, (string) $this->analytics); + } + } +} diff --git a/tests/SeoMetaTest.php b/tests/SeoMetaTest.php index 1114dce..80bade0 100644 --- a/tests/SeoMetaTest.php +++ b/tests/SeoMetaTest.php @@ -274,4 +274,20 @@ public function it_can_render_add_reset_webmasters() $this->assertContains($excepted, $this->seoMeta->render()); $this->assertContains($excepted, (string) $this->seoMeta); } + + /** @test */ + public function it_can_set_and_render_google_analytics() + { + $this->assertContains( + "ga('create', 'UA-12345678-9', 'auto');", + $this->seoMeta->render() + ); + + $this->seoMeta->setGoogleAnalytics('UA-98765432-1'); + + $this->assertContains( + "ga('create', 'UA-98765432-1', 'auto');", + $this->seoMeta->render() + ); + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 4b37dba..bf9a33b 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -80,6 +80,11 @@ protected function getEnvironmentSetUp($app) 'pinterest' => 'site-verification-code', 'yandex' => 'site-verification-code', ]); + + // Analytics + $app['config']->set('seo-helper.analytics', [ + 'google' => 'UA-12345678-9', + ]); } /**