我使用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仪表板上添加的任何自定义变量。我做错了什么?谢谢!
发布于 2014-12-02 05:43:19
在函数中,您应该使用window._paq而不是_paq。
发布于 2020-04-30 17:32:08
我假设您已经在< pageView > ..< /head >标记中放入/插入了pageView跟踪代码。代码可能如下所示(根据最新的matomo跟踪代码将_paq更改为window._paq ):
<script> var _paq = window._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
</script> 在此之前,您的代码将工作得很好,并将捕获数据。但是,当您尝试在函数中包含此代码时,Matomo将停止跟踪站点上的访问量。要避免这种情况,只需修改代码,如下所示:
如果您想将此跟踪代码放在函数内部,则在函数外部定义_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> 尝试上面的解决方案,它对我有效。如果你仍然面临这个问题,请让我知道。
https://stackoverflow.com/questions/22073699
复制相似问题