Skip to content

Commit

Permalink
Update to release v1.3.0 fixes #14 and fixes #15
Browse files Browse the repository at this point in the history
  • Loading branch information
kartik-v committed Jan 16, 2015
1 parent ee69acf commit ea6f3d0
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 50 deletions.
12 changes: 11 additions & 1 deletion CHANGE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version 1.3.0
=============
**Date:** 15-Jan-2015
**Date:** 16-Jan-2015

- (enh #6): Remove Facebook PHP SDK support (old version v3.0).
- (enh #10): German translations added
Expand All @@ -16,6 +16,16 @@ version 1.3.0
- Method `getFbApi`has been removed from the social module
- Module `getFbUser` has been removed from the social module
- Set release to stable.
- (enh #14): Add new configuration for GoogleAnalytics `anonymizeIp`.
- (enh #15): Various enhancements and additional configuration settings for the `GoogleAnalytics` widget:
- Remove support for old version (widget only supports the new Google Universal Analytics plugin)
- New `testMode` property that defaults to `YII_DEBUG` definition to allow google analytics to work from localhost.
- Ability to configure tracking object name. Defaults to `__gaTracker`.
- Ability to configure `trackerConfig` settings for creation of the ga object.
- Ability to configure `sendConfig` settings for sending of the ga data.
- Ability to configure `anonymizeIp` property.
- Ability to add additional javascript **before** sending the ga data.
- Ability to add additional javascript **after** sending the ga data.

version 1.2.0
=============
Expand Down
69 changes: 63 additions & 6 deletions GoogleAnalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use yii\base\InvalidConfigException;
use yii\helpers\Html;
use yii\helpers\Json;

/**
* Widget to embed Google Analytics tracking plugin on your website
Expand All @@ -27,6 +28,9 @@
*/
class GoogleAnalytics extends Widget
{
const HIT_NONE = 0;
const HIT_ALL = 1;
const HIT_EACH = 2;

/**
* @var string the Google Analytics Tracking ID
Expand All @@ -37,16 +41,53 @@ class GoogleAnalytics extends Widget
* @var string the domain name of your website where the tracking code will be displayed
*/
public $domain;

/**
* @var string the global object name. Defaults to `__gaTracker`.
*/
public $objectName = '__gaTracker';

/**
* @var bool whether to enable test mode to automatically set `cookieDomain`.
* The test mode will auto default to `YII_DEBUG` definition.
*/
public $testMode = YII_DEBUG;

/**
* @var int the settings to anonymize the IP address of the hit (http request)
* sent to Google Analytics. One of the following values are supported:
* - `0` or `GoogleAnalytics::HIT_NONE`: The IP will not be anonymized.
* - `1` or `GoogleAnalytics::HIT_ALL`: Anonymize the IP addresses for all the
* hits sent from a page (the lifetime of the tracker object)
* - `2` or `GoogleAnalytics::HIT_EACH`: Anonymize the IP address of an individual hit
*/
public $anonymizeIp = self::HIT_NONE;

/**
* @var array|string the tracker object configuration. Set it as a string (to be used as is)
* or an associative array of `$key => $value`.
*
* @see https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#create
*/
public $trackerConfig = [];

/**
* @var array|string the configuration for sending data. Set it as a string (to be used as is)
* or an associative array of `$key => $value`.
*
* @see https://developers.google.com/analytics/devguides/collection/analyticsjs/advanced#send
*/
public $sendConfig = [];

/**
* @var boolean whether to insert the new version of the google analytics tracking code
* @var string any javascript to prepend before the script that sends data
*/
public $newVersion = true;
public $jsBeforeSend = '';

/**
* @var boolean whether to insert the older version of the google analytics tracking code
* @var string any javascript to append after the script that sends data
*/
public $oldVersion = false;
public $jsAfterSend = '';

/**
* Initialize the widget
Expand All @@ -64,11 +105,27 @@ public function init()
if (empty($this->domain)) {
throw new InvalidConfigException("Google analytics tracking 'domain' has not been set.");
}
if ($this->anonymizeIp === self::HIT_ALL) {
$this->jsBeforeSend .= "{$this->objectName}('set', 'anonymizeIp', true);\n";
} elseif ($this->anonymizeIp === self::HIT_EACH && !is_string($this->sendConfig)) {
$this->sendConfig['anonymizeIp'] = true;
}
if ($this->testMode === true && !is_string($this->trackerConfig)) {
$this->trackerConfig['cookieDomain'] = 'none';
}
$trackerConfig = empty($this->trackerConfig) ? '' : ', ' .
(is_array($this->trackerConfig) ? Json::encode($this->trackerConfig) : $this->trackerConfig);
$sendConfig = empty($this->sendConfig) ? '' : ', ' .
(is_array($this->sendConfig) ? Json::encode($this->sendConfig) : $this->sendConfig);
$params = [
'id' => $this->id,
'domain' => $this->domain,
'newVersion' => $this->newVersion,
'oldVersion' => $this->oldVersion,
'anonIp' => $this->anonymizeIp,
'obj' => $this->objectName,
'trackerConfig' => $trackerConfig,
'sendConfig' => $sendConfig,
'jsBeforeSend' => $this->jsBeforeSend,
'jsAfterSend' => $this->jsAfterSend,
'noscript' => $this->renderNoScript()
];
echo $this->render('google-analytics', $params);
Expand Down
7 changes: 4 additions & 3 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,12 @@ public function initFbSession($params = [])
*
* @throws InvalidConfigException
*/
public function getFbSession($source)
public function getFbSession($params)
{
if (isset($this->_fbSession)) {
return $this->_fbSession;
}
$source = ArrayHelper::remove($params, 'source', '');
if (
empty($source) || !is_string($source) ||
!($source instanceof FacebookRedirectLoginHelper) ||
Expand All @@ -181,8 +182,8 @@ public function getFbSession($source)
/**
* Gets the Yii modified redirect login helper
*
* @param string $url the absolute url to redirect to
* @param array $params should be set as $key=>$value,
* @param string $url the url to redirect to
* @param array $paramss hould be set as $key=>$value,
* where $key is one of:
* - 'appId': string, the facebook application id (if not set, this
* will default from module facebook settings)
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ Module that enables access to social plugins for Yii Framework 2.0. It includes
- Fork Button
- Follow Button

> NOTE: Facebook PHP SDK v4.0 support is included since release v1.3.0 of the module. In case you have issues with Facebook PHP not being installed properly by composer, you would need to check your composer install logs or check to see if you have the right version. You may otherwise face issues of Facebook classes not found by the extension. In this situation, you may need to refresh your packages correctly (or overwrite the `vendor/facebook/php-sdk-4` folder with the downloaded facebook zip or tarball from [the github repo](https://github.com/facebook/facebook-php-sdk-v4/)).
## Installation

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
],
"minimum-stability": "stable",
"require": {
"facebook/php-sdk-v4": "*"
"facebook/php-sdk-v4": "@dev"
},
"autoload": {
"psr-4": {
Expand Down
54 changes: 17 additions & 37 deletions views/google-analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,22 @@
* @since 1.0
*/
?>
<?php if ($newVersion): ?>
<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');
<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', '<?= $obj ?>');

ga('create', '<?= $id ?>', '<?= $domain ?>');
ga('send', 'pageview');

</script>
<?php endif; ?>

<?php if ($oldVersion): ?>
<script type="text/javascript">//<![CDATA[
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '<?= $id ?>']);
_gaq.push(['_setDomainName', '<?= $domain ?>']);
_gaq.push(['_trackPageview']);
(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';

var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
//]]>
</script>
<?php endif; ?>
<?= $obj ?>('create', '<?= $id ?>', '<?= $domain ?>'<?= $trackerConfig ?>);
<?= $jsBeforeSend ?>
<?= $obj ?>('send', 'pageview'<?= $sendConfig ?>);
<?= $jsAfterSend ?>
</script>
<?= $noscript ?>

0 comments on commit ea6f3d0

Please sign in to comment.