Ajax paging and outgoing links tracking

Dear all,

on our website we are using the async piwik tracker and the setLinkClasses method to record some clicks as outgoing, as per documentation. Everything worked well (for several months!) until we added ajax paging to the site.

All the links added to the page with the ajax call are “not” recorded by piwik, even though the correct class is set on the elements. I did not find any mention of ajax in the documentation, hence I am not sure if we are doing something wrong or if this is not supported.

Thanks a lot for your help!

As a temporary (I hope) workaround we are adding onlclick=“javascript:_paq.push([‘trackLink’, ‘/the_link.html’, ‘link’]);” to the elements that we add via ajax.

You’re facing a pretty typical problem that arises from Ajax and JavaScript tracking. If Ajax isn’t loading the header and footer, you can’t put your JavaScript tracking code there and expect it to work. Some additional notes/options:
[li] You could (theoretically) move the tracking code from the header/footer into the body so it’s loaded with every ajax call. Unfortunately scripts aren’t necessarily loaded (see javascript - Executing <script> inside <div> retrieved by AJAX - Stack Overflow for info and work-arounds). Untested so it may also create issues inside JavaScript. Not necessarily recommended.
[/li][li] If you control the Ajax-loading code, stick it in there. On a successful reply from the server, register the pageview. Make sure you find all the instances and explicitly set titles and URLs and the like. I was working on an app with several Ajax functions (get, post, search, etc.) so I had to hack it into several parts of the JavaScript. This may be less feasible if you’re using a framework to automate Ajax calls.
Likely not exhaustive, but some additional points to consider.