Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add anonymize ip option for google analytics #8417

Merged
merged 3 commits into from
Feb 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion app/code/Magento/GoogleAnalytics/Block/Ga.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,13 @@ public function getPageTrackingCode($accountId)
$optPageURL = ", '" . $this->escapeHtmlAttr($pageName, false) . "'";
}

$anonymizeIp = "";
if ($this->_googleAnalyticsData->isAnonymizedIpActive()) {
$anonymizeIp = "\nga('set', 'anonymizeIp', true);";
}

return "\nga('create', '" . $this->escapeHtmlAttr($accountId, false)
. ", 'auto');\nga('send', 'pageview'{$optPageURL});\n";
. ", 'auto');{$anonymizeIp}\nga('send', 'pageview'{$optPageURL});\n";
}

/**
Expand All @@ -105,6 +110,7 @@ public function getOrdersTrackingCode()
$result = [];

$result[] = "ga('require', 'ec', 'ec.js');";

foreach ($collection as $order) {
if ($order->getIsVirtual()) {
$address = $order->getBillingAddress();
Expand Down
14 changes: 14 additions & 0 deletions app/code/Magento/GoogleAnalytics/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper

const XML_PATH_ACCOUNT = 'google/analytics/account';

const XML_PATH_ANONYMIZE = 'google/analytics/anonymize';

/**
* Whether GA is ready to use
*
Expand All @@ -34,4 +36,16 @@ public function isGoogleAnalyticsAvailable($store = null)
$accountId = $this->scopeConfig->getValue(self::XML_PATH_ACCOUNT, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store);
return $accountId && $this->scopeConfig->isSetFlag(self::XML_PATH_ACTIVE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store);
}

/**
* Whether anonymized IPs are active
*
* @param null|string|bool|int|Store $store
* @return bool
*/
public function isAnonymizedIpActive($store = null) {
$anonymize = $this->scopeConfig->getValue(self::XML_PATH_ANONYMIZE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store);
return $anonymize;
}

}
8 changes: 8 additions & 0 deletions app/code/Magento/GoogleAnalytics/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
<field id="*/*/active">1</field>
</depends>
</field>

<field id="anonymize" translate="label" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Anonymize IP</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<depends>
<field id="*/*/active">1</field>
</depends>
</field>
</group>
</section>
</system>
Expand Down