Javascript piwik setCustomVariable不工作

Javascript piwik setCustomVariable不工作,javascript,matomo,Javascript,Matomo,我使用piwik来跟踪我的网页访问,它工作得很好。 我刚刚在页面上的javascript中添加了以下代码: var _paq = _paq || []; _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { var u=(("https:" == document.location.protocol) ? "https" : "http")

我使用piwik来跟踪我的网页访问,它工作得很好。 我刚刚在页面上的javascript中添加了以下代码:

var _paq = _paq || [];
      _paq.push(['trackPageView']);
      _paq.push(['enableLinkTracking']);
      (function() {
        var u=(("https:" == document.location.protocol) ? "https" : "http") + "mypiwiklink";
        _paq.push(['setTrackerUrl', u+'piwik.php']);
        _paq.push(['setSiteId', 1]);
        var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript';
        g.defer=true; g.async=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
       })();
      // end piwik track code
这段代码没有问题。这是标准的。 现在我想添加一个自定义变量,用于跟踪页面上特定函数的每次调用。 在函数的代码中,我添加了:

   var selectTableRowHandler = function() {

        // function code

        //piwik code inside the function code:

                                 var _paq = _paq || [];
                                _paq.push(['setCustomVariable',  
                                    1, 
                                    "Visitor",
                                    "myfile", 
                                    "page" 
                                    ]);
                                  _paq.push(['trackPageView']);
                                  _paq.push(['enableLinkTracking']);
                                  (function() {
                                    var u=(("https:" == document.location.protocol) ? "https" : "http") + "mypiwiklink";
                                    _paq.push(['setTrackerUrl', u+'piwik.php']);
                                    _paq.push(['setSiteId', 1]);
                                    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript';
                                    g.defer=true; g.async=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
                                  })();


// end of function code here

}
调试时,我看到代码执行时没有错误,但我看不到在piwik仪表板上添加的任何自定义变量。 我做错了什么?
谢谢

在函数内部,您应该使用
窗口。\u paq
而不是
\u paq

我假设您已将页面视图跟踪代码放入/插入标记中。中。代码可能如下所示(更改为“窗口”。\u-paq根据最新的马托莫跟踪代码)):

var|paq=window.|paq|[];
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(功能(){
var u=((“https:==document.location.protocol)?“https:“http”)+“mypiwiklink”;
_push(['settrackerrl',u+'piwik.php']);
_paq.push(['setSiteId',1]);
var d=document,g=d.createElement('script'),s=d.getElementsByTagName('script')[0];g.type='text/javascript';
g、 defer=true;g.async=true;g.src=u+'piwik.js';s.parentNode.insertBefore(g,s);
})(); // 结束piwik轨迹代码
到目前为止,您的代码将运行良好,并将捕获数据。但是,当您尝试在函数中包含此代码时,Matomo将停止跟踪站点上的访问。要避免这种情况,只需按如下方式修改代码:

<script> 
//define _paq outside the function. Its scope should not be limited up to this function only. It will be used to track individual events as well in the body tag.
var _paq = window._paq || [];  
var selectTableRowHandler = function() {

    // function code

    //piwik code inside the function code:


                            _paq.push(['setCustomVariable',  
                                1, 
                                "Visitor",
                                "myfile", 
                                "page" 
                                ]);
                              _paq.push(['trackPageView']);
                              _paq.push(['enableLinkTracking']);
                              (function() {
                                var u=(("https:" == document.location.protocol) ? "https" : "http") + "mypiwiklink";
                                _paq.push(['setTrackerUrl', u+'piwik.php']);
                                _paq.push(['setSiteId', 1]);
                                var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript';
                                g.defer=true; g.async=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
                              })();// end of function code here}     
 </script> 
如果要将此跟踪代码放在函数内,则在函数外定义_paq,如下所示:

<script> 
//define _paq outside the function. Its scope should not be limited up to this function only. It will be used to track individual events as well in the body tag.
var _paq = window._paq || [];  
var selectTableRowHandler = function() {

    // function code

    //piwik code inside the function code:


                            _paq.push(['setCustomVariable',  
                                1, 
                                "Visitor",
                                "myfile", 
                                "page" 
                                ]);
                              _paq.push(['trackPageView']);
                              _paq.push(['enableLinkTracking']);
                              (function() {
                                var u=(("https:" == document.location.protocol) ? "https" : "http") + "mypiwiklink";
                                _paq.push(['setTrackerUrl', u+'piwik.php']);
                                _paq.push(['setSiteId', 1]);
                                var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript';
                                g.defer=true; g.async=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
                              })();// end of function code here}     
 </script> 

//在函数外部定义paq。其范围不应仅限于此功能。它也将用于跟踪body标签中的单个事件。
var | paq=窗口。|paq | |[];
var selectTableRowHandler=函数(){
//功能代码
//功能代码中的piwik代码:
_paq.push(['setCustomVariable',
1.
“访客”,
“我的文件”,
“第页”
]);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(功能(){
var u=((“https:==document.location.protocol)?“https:“http”)+“mypiwiklink”;
_push(['settrackerrl',u+'piwik.php']);
_paq.push(['setSiteId',1]);
var d=document,g=d.createElement('script'),s=d.getElementsByTagName('script')[0];g.type='text/javascript';
g、 defer=true;g.async=true;g.src=u+'piwik.js';s.parentNode.insertBefore(g,s);
})();//此处函数代码结尾}

试试上面的解决方案,它对我有效。如果您仍然面临此问题,请告诉我。

您是否尝试过
var\u paq=\u paq\124;[]在函数之外,我不确定,但我认为它需要与piwik.jsk在同一范围内。。。您是否在请求中看到一个
\u cvars
GET参数?我建议使用带有“网络”功能的Firebug。看看从你的机器发送到piwik的请求。对于来自Piwik的direkt响应以及它所看到的内容,您可以将Piwik请求的位置复制到一个新选项卡中,在GET参数中的某个位置添加
debug=1
,然后执行它。首先,不要忘记将
[Tracker]\r debug\u on_demand=1
添加到
路径/to/piwik/public/config/config.ini.php
中,然后获得调试输出。