Can a specific route include specific js - Piwik tracking


  • Admin

    Haha, didn't I do that on the code I edited for you above?



  • @psychobunny Yes, yes, but I wasn't pasting back the entire bit. I was just looking for the diff.

    That console error is resolved but now I get an "undefined" for data.tpl_url



  • This is what it looks like now, I added a couple of console.log commands to see what is going on:

    <script type="text/javascript">
    var _paq = _paq || [];
    (function () {
        var u = "//my.piwikcollector.com/";
    
        function firePiwik (data) {
            if (app.user && app.user.uid > 0) {
                _paq.push(['setUserId', app.user.uid.toString()]);
                _paq.push(['setCustomVariable', 1, "appUserUsername", app.user.username, "visit"]);
            }
            _paq.push(['setDocumentTitle', document.title]);
            _paq.push(['setCustomUrl', location.href]);
            _paq.push(['enableHeartBeatTimer']);
            _paq.push(['appendToTrackingUrl', 'bots=1']);
    		if (data.tpl_url === 'search') {
    			_paq.push(['trackSiteSearch', "test", "test", "false"]);
    			console.log('in search ' + data.tpl_url);
    		} else {
    			_paq.push(['trackPageView']);
    			console.log('not in search ' + data.tpl_url);
    		}
            _paq.push(['enableLinkTracking']);
            _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.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
    
        $(window).on('action:ajaxify.end', function(ev, data) {
            firePiwik(data);
        });
    })();
    </script>
    

  • Plugin & Theme Dev

    @rod apparently there is no tpl_url in data 😄

    use if (data.tpl === 'search')



  • @pichalite and @psychobunny Yes, yes. Now it is working better. Thank you.

    How do I see what other variables are available specifically related to the search. Really my code bit needs to report to Piwik the search term and hopefully the result count.

    _paq.push(['trackSiteSearch', searchTerm,, resultCount]);

    Are those two variables available to me?

    Thank you.


  • Plugin & Theme Dev

    @rod add a console.log(data); to your code and you can see what's in the data object in your browser console

    the data object has url and tpl

    if there is a search query, then the url will look like this
    search/welcome?in=titlesposts&sortBy=timestamp&sortDirection=desc&showAs=posts

    so the part between search/ and the ? is the search term. welcome in this case.



  • @pichalite Are there any other objects that would report back the number of results? The actual search page does return the number. Is that available to my js code bit?



  • Looking at the source for NodeBB on github in search.js I see this:

    if (data.searchIn === 'posts' || data.searchIn === 'titlesposts') {
    					search.searchQuery('post', data.query, results.searchCids, results.searchUids, next);
    

    Is the data.query object available to me?


  • Plugin & Theme Dev

    @rod yup, should be available in ajaxify.data.matchCount



  • @pichalite that worked like a champ. I was hoping that ajaxify.data.query would also work but it did not. 😦


  • Plugin & Theme Dev

    @rod it is available in ajaxify.data, use ajaxify.data.search_query



  • @pichalite And voila!

    _paq.push(['trackSiteSearch', ajaxify.data.search_query,, ajaxify.data.matchCount]);

    Piwik now gets the search query and the number of results.

    And, yup, there it was on line 25 staring me in the face:
    result.search_query = validator.escape(data.query);

    Thanks for helping an 'old guy out.



  • I celebrated just a bit too soon. I think I need to tweak it a bit. Piwik isn't getting the count correct. Recording "1" for all returns, more than 1 and even for zero. I'll keep chipping away at this.



  • All is good. The correct information is being fed to Piwik. Thanks all for your help.


  • Admin

    If you are interested, it would be great if you wrote a guide to set up Piwik for NodeBB 🙂

    I'm thinking of adding a "tutorials" category for stuff like this



  • @psychobunny Considering how helpful this community has been it would be my pleasure to contribute back in this tiny way. I will take ownership of writing up a document on how to make Piwik and NodeBB work in harmony.


  • Admin

    Awesome 🙂 I went ahead and created a tutorials category, hope to see that full of guides in the near future


 

| |