404 tracking with asynchronous tracking code

Hi guys,

Trying to get my 404 page tracking with the asynchronous code however when I add the code detailed here it fully stops my ability to track. When I add that code tracking stops all together.

Any ideas?

I also have the current old Piwik tracking code which has some custom PHP stuff, how would I go ahead and switch it over?

<!-- Piwik --> 
<script type="text/javascript">
var pkBaseURL = (("https:" == document.location.protocol) ? "https://stats.example.com/" : "http://stats.example.com/");
document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
</script><script type="text/javascript">
try {
var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 18);
$category = get_the_category();
if (!is_front_page() && !empty($category[1])) {
    echo 'piwikTracker.setCustomVariable(1, "Category", "'.$category[1]->cat_name.'", "page");
    echo 'piwikTracker.setCustomVariable(2, "Sub-Category", "'.$category[0]->cat_name.'", "page");';
elseif (!is_front_page() && !empty($category[0])) {
    echo 'piwikTracker.setCustomVariable(1, "Category", "'.$category[0]->cat_name.'", "page");';
if (is_404()) {
	echo 'piwikTracker.setDocumentTitle(\'404/URL = \'+String(document.location.pathname+document.location.search).replace(/\//g,"%2f") + \'/From = \' + String(document.referrer).replace(/\//g,"%2f"));';

Have you tried the official asynchronous solution ? How to - Analytics Platform - Matomo

please report if it does not work, but hopefully it does?

Thats what I linked to haha. I’m trying that, but it isn’t working on a normal html page. It just stops tracking all together.

Sorry, there was a typo in the code in that FAQ entry. I’ve fixed it, now. If you add a ‘(’ right after ‘_paq.push’ and right before ‘[’, the code will work.

Sorry, I believe the script should actually look like this:

_paq.push(['setDocumentTitle', '404/URL = '+String(document.location.pathname+document.location.search).replace(/\//g,"%2f") + '/From = ' + String(document.referrer).replace(/\//g,"%2f")]);

Notice the extra \ before the ///g’s

Yes, you’re right! I’ve made the extra change, thanks for the reply!