2

UPDATE: Hopefully this is a better explanation of the problem:

I'm trying to pass on the product SKU on my product details page to Google Analytics using _setCustomVar. I'm running on Magento 1.4.0.1 and my Analytics async code is inserted by the default GA module in the <head> section and it looks like this:

<script type="text/javascript">

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxxx-1']);
_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>

The custom variable I'm trying to add has this syntax:

_gaq.push(['_setCustomVar',1,'View Product','<?php echo $_helper->productAttribute($_product, $_product->getSku(), 'sku') ?>',3]);

According to the Analytic documentation, in order for the custom variable to be recorded, the _setCustomVar must be called BEFORE the _trackPageView, but there's no support for this in the default GoogleAnalytics module. There are 2 issues to this problem:

  1. How can I add my _setCustomVar function BEFORE the default tracking code?
  2. How can I add my _setCustomVar function ONLY on product pages?

Original post:

I'm trying to store the SKU of the product being viewed by a visitor in a Analytics Custom Variable. The syntax for this is _gaq.push(['_setCustomVar',3,'View Product','SKU12345',2]);.

Obviously this snippet of code should be added only to the product detail pages, and not to the list, cart, or checkout pages. So I've tried editing the view.phtml file in app/design/frontend/default/my_package/template/catalog/product by adding the following piece of code:

<script>
_gaq.push(['_setCustomVar',
    1,
    'View Product',
    '<?php echo $_helper->productAttribute($_product, $_product->getSku(), 'sku') ?>', 
    3]);
</script>

The problem is that the I'm adding this custom variable AFTER the basic tracking code, which is added by default in the <head> section, so it doesn't get recorded in Analytics.

I tried to avoid altering the core files with the Analytics module in app/code/core/Mage/GoogleAnalytics/Block/Ga.php, but I think the solution may lay there. How can I add the piece of code that sets the custom variable so that it appears within the basic tracking code BEFORE _gaq.push(['_trackPageview']);?

This is my async code provided by Analytics:

<script type="text/javascript">

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxxx-1']);
_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>

Idea from here

Note: I'm using Magento 1.4.0.1 and the Analytics Asynchronous Syntax

4

3 に答える 3

2

これは現在、magento サイトの 1 つで作業しています。Magento Admin Google API を使用している場合は、(1) カスタム モジュールを作成して拡張するか、(2) (ソースを表示するときに) <script>_gaq.push(['_setCustomVar...javascript が var の下にあることを確認してください。 _gaq = _gaq || []; コードブロック

<script type="text/javascript">
  //<![CDATA[
    var _gaq = _gaq || [];

    _gaq.push(['_setAccount', 'UA-xxxxxxx-3']);
    _gaq.push(['_trackPageview']);
    _gaq.push(['_setCustomVar', '1', 'awe2', '<?php echo $_helper->productAttribute($_product, $_product->getSku(), 'sku') ?>', '1']);

    (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>

カスタム モジュールを作成するには

app/code/local/MageIgniter/GoogleAnalytics/etc/config.xml 内

  <config>
    <modules>
        <MageIgniter_GoogleAnalytics>
            <version>0.1.0</version>
        </MageIgniter_GoogleAnalytics>
    </modules>
    <global>
        <blocks>
            <googleanalytics>
                <rewrite>
                    <ga>MageIgniter_GoogleAnalytics_Block_Ga</ga>
                </rewrite>
            </googleanalytics>
        </blocks>
    </global>
  </config>

/app/code/local/MageIgniter/GoogleAnalytics/Block/Ga.php に作成

    class MageIgniter_GoogleAnalytics_Block_Ga extends Mage_GoogleAnalytics_Block_Ga
    {

        private $remId = NULL;
        private $conversionId = NULL;
        private $conversionLabel = NULL;


        public function getPageName()
        {
            return $this->_getData('page_name');
        }


        protected function _getPageTrackingCode($accountId)
        {
            $pageName   = trim($this->getPageName());
            $optPageURL = '';
            if ($pageName && preg_match('/^\/.*/i', $pageName)) {
                $optPageURL = ", '{$this->jsQuoteEscape($pageName)}'";
            }
            return "
    _gaq.push(['_setAccount', '{$this->jsQuoteEscape($accountId)}']);
    _gaq.push(['_trackPageview'{$optPageURL}]);
    " . $this->getProductSku();
        }

      ......

        public function getProductSku(){
            if($product = Mage::registry('current_product')){
               return sprintf("_gaq.push(['_setCustomVar', '%s', '%s', '%s', '%s']);",
                    1,
                    'Sku',
                    $product->getSku(),
                    1
                ) . "\n";
            }

            return '';
        }

     ........
 }

詳細については、/app/code/core/Mage/GoogleAnalytics/Block/Ga.php を参照してください。

于 2012-10-29T12:24:08.300 に答える
1

デフォルトの GoogleAnalytics モジュールを変更することで、なんとか機能させることができました。

app/code/core/Mage/GoogleAnalytics/Blockファイルをコピーし、移動Ga.php/www/app/code/local/Mage/GoogleAnalytics/Blockて貼り付けます (必要なフォルダーが存在しない場合は作成します)。

新しいGa.phpで、_toHtml()Analytics が Magento バックエンドからアクティブ化されているかどうかをテストした後の関数で、現在のページが製品の詳細ページであるかどうかをテストします。

$_product = Mage::registry('current_product');  
if($_product) {
        //output code with _setCustom var
    } else {
        //output normal tracking code
    }

カスタム変数が設定されたコードは次のようになります。

$this->addText('
<!-- BEGIN GOOGLE ANALYTICS CODE -->
<script type=\"text/javascript\">
var _gaq = _gaq || [];
_gaq.push([\'_setAccount\', \'UA-25272379-1\']);
_gaq.push([\'_setCustomVar\', 1, \'Product View\',\''.$_product->getSku().'\', 3]);
_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 -->
');
于 2012-10-29T20:31:00.220 に答える
0

Google アナリティクスの非同期挿入コードを使用すると、このコードをどこに配置しても、ページの読み込みが完了したときにのみ適用されます。_gaq.push()したがって、HTML コードにインラインである限り (非同期ではない)、after と before を追加できます。

非同期コードは次のようになります。

(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);
})();
于 2012-10-29T09:34:42.157 に答える