Magento 1.4.1.1
回溯历史,app/code/core/Mage/GoogleAnalytics/Block/Ga.php被复制到app/code/local/Mage/GoogleAnalytics/Block/Ga.php并进行了更改,以便该protected function _toHtml() {}
函数可以生成正确的 Google页面上的分析异步 javascript 条目。原版存在未针对该网站正确向 Google 发布跟踪信息的问题。此重写功能完全正常,并获得了预期的结果。
为了清理,决定将此更改模块化为本地命名空间模块。创建了以下文件...
应用程序/etc/modules/Chief_GoogleAnalytics.xml
<?xml version="1.0"?>
<config>
<modules>
<Chief_GoogleAnalytics>
<active>true</active>
<codePool>local</codePool>
</Chief_GoogleAnalytics>
</modules>
</config>
应用程序/代码/本地/Chief/GoogleAnalytics/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Chief_GoogleAnalytics>
<version>0.1.0</version>
</Chief_GoogleAnalytics>
</modules>
<global>
<blocks>
<googleanalytics>
<rewrite>
<ga>Chief_GoogleAnalytics_Block_Ga</ga>
</rewrite>
</googleanalytics>
</blocks>
</global>
</config>
app/code/local/Chief/GoogleAnalytics/Block/Ga.php
/**
* GoogleAnalitics Page Block
*
* @category Chief
* @package Chief_GoogleAnalytics
* @author Magento Core Team <core@magentocommerce.com>
*/
class Chief_GoogleAnalytics_Block_Ga extends Mage_GoogleAnalytics_Block_Ga
{
/**
* Prepare and return block's html output
*
* @return string
*/
protected function _toHtml()
{
if (!Mage::getStoreConfigFlag('google/analytics/active')) {
return '';
}
$this->addText('
<!-- BEGIN GOOGLE ANALYTICS CODE -->
<script type="text/javascript">
//<![CDATA[
var _gaq = _gaq || [];
_gaq.push(["_setAccount", "' . $this->getAccount() . '"]);
_gaq.push(["_trackPageview", "'.$this->getPageName().'"]);
(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>
<!-- END GOOGLE ANALYTICS CODE -->
');
$this->addText($this->getQuoteOrdersHtml());
if ($this->getGoogleCheckout()) {
$protocol = Mage::app()->getStore()->isCurrentlySecure() ? 'https' : 'http';
$this->addText('<script src="'.$protocol.'://checkout.google.com/files/digital/ga_post.js" type="text/javascript"></script>');
}
return parent::_toHtml();
}
}
当它来自app/code/local/Mage/GoogleAnalytics/Block/Ga.php时,而不是正常的单个更正脚本条目,我现在得到一个重复。Chief_GoogleAnalytics Block 覆盖首先出现,然后是来自app/code/core/Mage/GoogleAnalytics/Block/Ga.php的旧股票垃圾代码。Google Analytics 有一个布局 XML 文件,将其定义为在after_body_start中显示,这样就可以了。这是显示新块覆盖和旧块的重复项。是什么赋予了?
<!-- BEGIN GOOGLE ANALYTICS CODE -->
<script type="text/javascript">
//<![CDATA[
var _gaq = _gaq || [];
_gaq.push(["_setAccount", "UA-xxxxxx-x"]);
_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>
<!-- END GOOGLE ANALYTICS CODE -->
<!-- BEGIN GOOGLE ANALYTICS CODE -->
<script type="text/javascript">
//<![CDATA[
(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';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
})();
var _gaq = _gaq || [];
_gaq.push(["_setAccount", "UA-xxxxxx-x"]);
_gaq.push(["_trackPageview", "/"]);
//]]>
</script>
<!-- END GOOGLE ANALYTICS CODE -->
加载块的 page.phtml 条目。
<?php echo $this->getChildHtml('after_body_start') ?>
googleanalytics.xml 布局
<layout version="0.1.0">
<default>
<!-- Mage_GoogleAnalytics -->
<reference name="after_body_start">
<block type="googleanalytics/ga" name="google_analytics" as="google_analytics" />
</reference>
</default>
</layout>
通过在战略位置使用Mage::Log()
with 添加取证
由于我们在这里传递一个数组对象,它比使用字符串要复杂一些。$this->getData()
_toHtml() 函数的开始
2012-06-24T21:02:04+00:00 DEBUG (7): Array
(
[type] => googleanalytics/ga
[module_name] => Chief_GoogleAnalytics
)
立即使其 parent::_toHtml();
加载 $original_output =parent::_toHtml
2012-06-24T21:02:04+00:00 DEBUG (7): Array
(
[type] => googleanalytics/ga
[module_name] => Chief_GoogleAnalytics
[account] => UA-xxxxxx-x
[page_name] => /
[text] =>
<!-- BEGIN GOOGLE ANALYTICS CODE -->
<script type="text/javascript">
//<![CDATA[
(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';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
})();
var _gaq = _gaq || [];
_gaq.push(["_setAccount", "UA-xxxxxx-x"]);
_gaq.push(["_trackPageview", "/"]);
//]]>
</script>
<!-- END GOOGLE ANALYTICS CODE -->
)
为了防止这变成一个 ;tldr,记录$this->getData();
最后返回的位置,吐出两个 javascript,但与原来的相反。
$this->getData();
在没有引用的情况下parent::_toHtml
传递以下内容,这是我们想要的返回对象。一个具有适当标识符的脚本...
2012-06-24T21:06:24+00:00 DEBUG (7): Array
(
[type] => googleanalytics/ga
[module_name] => Chief_GoogleAnalytics
[account] => UA-xxxxxx-x
[page_name] => /aircraft
[text] =>
<!-- BEGIN GOOGLE ANALYTICS CODE -->
<script type="text/javascript">
//<![CDATA[
var _gaq = _gaq || [];
_gaq.push(["_setAccount", "UA-xxxxxx-x"]);
_gaq.push(["_trackPageview", "/aircraft"]);
(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>
<!-- END GOOGLE ANALYTICS CODE -->
)