我使用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仪表板上添加的任何自定义变量。我究竟做错了什么?谢谢!
在功能内,您应该使用window._paq
而不是_paq
。
我假设您已经将pageView跟踪代码放入/插入了
.. head>标记中。该代码可能如下所示(根据最新的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>
尝试上述对我有用的解决方案。如果您仍然遇到问题,请告诉我。