Skip to content

Commit

Permalink
Adding Analytics Support
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanedev-maroc committed Oct 20, 2015
1 parent 8e06f86 commit 2c4c47d
Show file tree
Hide file tree
Showing 11 changed files with 325 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -52,14 +53,14 @@ SEO Helper is a package that provides tools and helpers for SEO (Search Engine O
## TODOS

- [ ] Complete the documentation.
- [ ] Adding Google Analytics support.

## DONE

- [x] Framework agnostic package.
- [x] Open Graph supported.
- [x] Twitter Cards supported.
- [x] Webmaster tools supported.
- [x] Google Analytics supported.
- [x] Laravel 5.1 supported.

## Contribution
Expand Down
8 changes: 8 additions & 0 deletions _docs/4-Usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 :
Expand Down
31 changes: 28 additions & 3 deletions _docs/5-API.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 5. API
# 5. API

## Table of contents

Expand All @@ -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)
Expand All @@ -18,7 +19,7 @@
* [SEO Meta](#seo-meta)
* [SEO Open Graph](#seo-open-graph)
* [SEO Twitter Card](#seo-twitter-card)

## 1. Contracts

### Renderable
Expand Down Expand Up @@ -216,7 +217,7 @@ interface DescriptionInterface extends Renderable
* @return self
*/
public function setMax($max);

/* ------------------------------------------------------------------------------------------------
| Main Functions
| ------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -407,6 +408,30 @@ interface WebmastersInterface extends Renderable
}
```

### Analytics

```php
<?php namespace Arcanedev\SeoHelper\Contracts\Entities;

use Arcanedev\SeoHelper\Contracts\Renderable;

interface AnalyticsInterface extends Renderable
{
/* ------------------------------------------------------------------------------------------------
| Getters & Setters
| ------------------------------------------------------------------------------------------------
*/
/**
* Set google analytics code.
*
* @param string $code
*
* @return self
*/
public function setGoogle($code);
}
```

### Open Graph

```php
Expand Down
8 changes: 8 additions & 0 deletions config/seo-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,12 @@
//
],
],

/* ------------------------------------------------------------------------------------------------
| Analytics
| ------------------------------------------------------------------------------------------------
*/
'analytics' => [
'google' => '', // UA-XXXXXXXX-X
],
];
25 changes: 25 additions & 0 deletions src/Contracts/Entities/AnalyticsInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php namespace Arcanedev\SeoHelper\Contracts\Entities;

use Arcanedev\SeoHelper\Contracts\Renderable;

/**
* Interface AnalyticsInterface
*
* @package Arcanedev\SeoHelper\Contracts\Entities
* @author ARCANEDEV <[email protected]>
*/
interface AnalyticsInterface extends Renderable
{
/* ------------------------------------------------------------------------------------------------
| Getters & Setters
| ------------------------------------------------------------------------------------------------
*/
/**
* Set google analytics code.
*
* @param string $code
*
* @return self
*/
public function setGoogle($code);
}
9 changes: 9 additions & 0 deletions src/Contracts/SeoMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
| ------------------------------------------------------------------------------------------------
Expand Down
113 changes: 113 additions & 0 deletions src/Entities/Analytics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php namespace Arcanedev\SeoHelper\Entities;

use Arcanedev\SeoHelper\Contracts\Entities\AnalyticsInterface;
use Arcanedev\Support\Traits\Configurable;

/**
* Class Analytics
*
* @package Arcanedev\SeoHelper\Entities
* @author ARCANEDEV <[email protected]>
*/
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 <<<EOT
<script>
(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');
</script>
EOT;
}
}
40 changes: 40 additions & 0 deletions src/SeoMeta.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Arcanedev\SeoHelper;

use Arcanedev\SeoHelper\Contracts\Entities\AnalyticsInterface;
use Arcanedev\SeoHelper\Contracts\Entities\DescriptionInterface;
use Arcanedev\SeoHelper\Contracts\Entities\KeywordsInterface;
use Arcanedev\SeoHelper\Contracts\Entities\MiscTagsInterface;
Expand Down Expand Up @@ -67,6 +68,13 @@ class SeoMeta implements Contracts\SeoMeta
*/
protected $webmasters;

/**
* The Analytics instance.
*
* @var AnalyticsInterface
*/
protected $analytics;

/* ------------------------------------------------------------------------------------------------
| Constructor
| ------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -102,6 +110,9 @@ private function init()
$this->webmasters(
new Entities\Webmasters($this->getConfig('webmasters', []))
);
$this->analytics(
new Entities\Analytics($this->getConfig('analytics', []))
);
}

/* ------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
| ------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -372,6 +411,7 @@ public function render()
$this->keywords->render(),
$this->misc->render(),
$this->webmasters->render(),
$this->analytics->render(),
]));
}

Expand Down
Loading

0 comments on commit 2c4c47d

Please sign in to comment.