Skip to content

Commit

Permalink
Merge pull request #37 from basz/feature/displayfeatures-2
Browse files Browse the repository at this point in the history
Feature/displayfeatures 2
  • Loading branch information
Jurian Sluiman committed May 26, 2014
2 parents 1222e5b + a3a2ab6 commit a374620
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 8 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,25 @@ $var = new SlmGoogleAnalytics\Analytics\CustomVariable($index, $name, $value,
```

The scope can be `SCOPE_VISITOR`, `SCOPE_SESSION` or (the default) `SCOPE_PAGE_LEVEL`.

### Display Advertising
To enable Google Analytics [Display Advertising](https://support.google.com/analytics/answer/3450482) features simply call the appropiate method on the tracker.

```php
$ga->setEnableDisplayAdvertising(true);
```

Or, alternatively, you can set these variables inside the configuration:

```php
'google_analytics' => array(
'enable_display_advertising' => true,
),
```


The Google Analytics Display Advertising features include the following:

- [Demographics and Interests reporting](https://support.google.com/analytics/answer/2799357)
- [Remarketing with Google Analytics](https://support.google.com/analytics/answer/2611268)
- DoubleClick Campaign Manager integration (for [Google Analytics Premium](https://www.google.com/intl/en_ALL/analytics/premium/index.html))
13 changes: 7 additions & 6 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@

return array(
'google_analytics' => array(
'enable' => true,
'id' => '',
'domain_name' => '',
'allow_linker' => false,
'anonymize_ip' => false,
'script' => 'google-analytics-ga',
'enable' => true,
'id' => '',
'domain_name' => '',
'allow_linker' => false,
'enable_display_advertising' => false,
'anonymize_ip' => false,
'script' => 'google-analytics-ga',
),
'service_manager' => array(
'aliases' => array(
Expand Down
9 changes: 9 additions & 0 deletions config/slmgoogleanalytics.global.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ $googleAnalytics = array(
'domain_name' => '',
'allow_linker' => false,

/**
* Enable Google's Analytics Display Advertising features which include the following:
*
* - Demographics and Interests reporting
* - Remarketing with Google Analytics
* - DoubleClick Campaign Manager integration (for Google Analytics Premium)
*/
// 'enable_display_advertising' => true,

/**
* Disable/enable page tracking
*
Expand Down
11 changes: 11 additions & 0 deletions src/SlmGoogleAnalytics/Analytics/Tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class Tracker
protected $enableTracking = true;
protected $enablePageTracking = true;
protected $allowLinker = false;
protected $enableDisplayAdvertising = false;
protected $domainName;
protected $anonymizeIp = false;
protected $customVariables = array();
Expand Down Expand Up @@ -113,6 +114,16 @@ public function getAllowLinker()
return $this->allowLinker;
}

public function setEnableDisplayAdvertising($enableDisplayAdvertising)
{
$this->enableDisplayAdvertising = $enableDisplayAdvertising;
}

public function getEnableDisplayAdvertising()
{
return $this->enableDisplayAdvertising;
}

public function setDomainName($domain_name)
{
$this->domainName = (string) $domain_name;
Expand Down
4 changes: 4 additions & 0 deletions src/SlmGoogleAnalytics/Service/TrackerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public function createService(ServiceLocatorInterface $serviceLocator)
$tracker->setAllowLinker($gaConfig['allow_linker']);
}

if (true === $gaConfig['enable_display_advertising']) {
$tracker->setEnableDisplayAdvertising(true);
}

if (true === $gaConfig['anonymize_ip']) {
$tracker->setAnonymizeIp(true);
}
Expand Down
12 changes: 12 additions & 0 deletions src/SlmGoogleAnalytics/View/Helper/Script/Analyticsjs.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function getCode()

$script .= $this->prepareCreate();
$script .= $this->prepareLinker();
$script .= $this->prepareDisplayAdvertising();
$script .= $this->prepareTrackEvents();
$script .= $this->prepareTransactions();
$script .= $this->prepareSend();
Expand Down Expand Up @@ -206,6 +207,17 @@ protected function prepareLinker()
return $output;
}

protected function prepareDisplayAdvertising()
{
$output = '';

if ($this->tracker->getEnableDisplayAdvertising()) {
$output .= $this->requirePlugin('displayfeatures');
}

return $output;
}

protected function prepareTrackEvents()
{
$events = $this->tracker->getEvents();
Expand Down
7 changes: 6 additions & 1 deletion src/SlmGoogleAnalytics/View/Helper/Script/Gajs.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,15 @@ public function getCode()

protected function getLoadScript()
{
$script = 'google-analytics.com/ga.js';
if (true === $this->tracker->getEnableDisplayAdvertising()) {
$script = 'stats.g.doubleclick.net/dc.js';
}

return <<<SCRIPT
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + '$script';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();\n
SCRIPT;
Expand Down
6 changes: 6 additions & 0 deletions tests/SlmGoogleAnalyticsTest/Analytics/TrackerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ public function testAllowLinkerDefaultsToFalse()
$this->assertTrue($tracker->getAllowLinker());
}

public function testEnableDisplayAdvertisingDefaultsToFalse()
{
$tracker = new Tracker(123);
$this->assertFalse($tracker->getEnableDisplayAdvertising());
}

public function testAnonymizeIpDefaultsToFalse()
{
$tracker = new Tracker(123);
Expand Down
24 changes: 23 additions & 1 deletion tests/SlmGoogleAnalyticsTest/View/Helper/GoogleAnalyticsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,29 @@ public function testHelperLoadsFileFromGoogle()
$script = <<<SCRIPT
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();\n
SCRIPT;

$output = $this->getOutput($this->helper);
$this->assertContains($script, $output);
}

public function testEnableDisplayFeaturesLoadDifferentFileFromGoogle()
{
$this->tracker->setEnableDisplayAdvertising(true);

$script = new Script\Gajs();
$script->setTracker($this->tracker);

$helper = new Helper($script);
$helper();

$script = <<<SCRIPT
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();\n
SCRIPT;
Expand Down

0 comments on commit a374620

Please sign in to comment.