• Home
  • Categories
  • Recent
  • Popular
  • Top
  • Tags
  • Users
  • Groups
  • Documentation
    • Home
    • Read API
    • Write API
    • Plugin Development
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
v3.5.2 Latest
Buy Hosting

In which version did Custom Javascript show up?

Scheduled Pinned Locked Moved Technical Support
23 Posts 3 Posters 1.9k Views
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • rodR Offline
    rodR Offline
    rod
    replied to julian on last edited by
    #6

    @julian Thanks Julian, my code doesn’t work their either but I don’t get the “$” error that I previously posted. @baris posted a response, I’ll give that a try. If I get it working in Custom Header I’ll try to move it to Custom JavaScript

    1 Reply Last reply
    0
  • rodR Offline
    rodR Offline
    rod
    replied to <baris> on last edited by rod
    #7

    @baris @julian That didn't help. My code snippet was just a piece of the javascript. Here is all of it, maybe that will help figuring out why the $(window)... bit no longer works since upgrading from 1.4.5

    var _paq = _paq || [];
    (function () {
        var u = "//your.piwikurlhere.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 === 'search') {
    			_paq.push(['trackSiteSearch', ajaxify.data.search_query,, ajaxify.data.matchCount]);
    		} else {
    			_paq.push(['trackPageView']);
    		}
            _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);
        });
    })();
    

    The problem is that pesky $(window) area. I tried to replace it with the suggestion you made but that also didn't work and spewed number of other console errors.

    I think you could just drop this whole code bit into your Custom Javascript area and see it in action. The URL won't be correct but that shouldn't matter.

    I really want to get this working so I can stay current with my NodeBB versions. I really do. 🙂

    Thanks!

    1 Reply Last reply
    0
  • barisB Offline
    barisB Offline
    <baris> NodeBB
    wrote on last edited by
    #8

    This should work in custom javascript area I think.

    var _paq = _paq || [];
    (function () {
        var u = "//your.piwikurlhere.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 === 'search') {
    			_paq.push(['trackSiteSearch', ajaxify.data.search_query,, ajaxify.data.matchCount]);
    		} else {
    			_paq.push(['trackPageView']);
    		}
            _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);
    
        // added this line
        firePiwik(ajaxify.data);
    
        $(window).on('action:ajaxify.end', function(ev, data) {
            firePiwik(data);
        });
    })();
    

    Let me know if you are getting any errors with this.

    rodR 1 Reply Last reply
    0
  • rodR Offline
    rodR Offline
    rod
    replied to <baris> on last edited by
    #9

    @baris said in In which version did Custom Javascript show up?:

    firePiwik(ajaxify.data);

    This has not solved the problem. Possibly closer, but not solved. I have my code back in Custom Javascript. I am not seeing errors for that in my browser console. 😞

    It now behaves like it just doesn't execute firePiwik(data); in the "$(window)" area. This is all so strange.

    Thanks.

    1 Reply Last reply
    0
  • barisB Offline
    barisB Offline
    <baris> NodeBB
    wrote on last edited by
    #10

    Can you put a console.log(data) inside function firePiwik (data) { and check your browser console. Does it print anything when you refresh the page?

    rodR 1 Reply Last reply
    0
  • rodR Offline
    rodR Offline
    rod
    replied to <baris> on last edited by
    #11

    @baris I added that to the function, I see data being displayed. Is there anything specific I should be looking for? I could give you access to the forum (it is a dev forum not prod) if you wanted to poke on it directly.

    1 Reply Last reply
    0
  • barisB Offline
    barisB Offline
    <baris> NodeBB
    wrote on last edited by
    #12

    If you see the data and if your <head> has the piwik.js file then I am not sure why it is not working.

    rodR 1 Reply Last reply
    0
  • rodR Offline
    rodR Offline
    rod
    replied to <baris> on last edited by
    #13

    @baris The odd thing is that it works in 1.4.5. Should it work if I move it to Custom Header with the script tags around it? Because when I try that I do get an error in the console and do NOT get any of that data form the console.log output.

    The error I get when it is in Custom Header is ReferenceError: Can't find variable: ajaxify

    1 Reply Last reply
    0
  • barisB Offline
    barisB Offline
    <baris> NodeBB
    wrote on last edited by
    #14

    If you put it in custom header you need to wrap it in

    window.addEventListener('load', function () {
    // rest of the code
    });

    so it waits for the scripts to load otherwise ajaxify will be undefined.

    rodR 1 Reply Last reply
    0
  • rodR Offline
    rodR Offline
    rod
    replied to <baris> on last edited by
    #15

    @baris You lost me on that last post. What section do I wrap in window.addEventListener('load', function ()?

    1 Reply Last reply
    0
  • barisB Offline
    barisB Offline
    <baris> NodeBB
    wrote on last edited by
    #16

    Everything. If you put it in custom header it should look like this

    <script>
    window.addEventListener('load', function () {
        var _paq = _paq || [];
        var u = "//your.piwikurlhere.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 === 'search') {
    			_paq.push(['trackSiteSearch', ajaxify.data.search_query,, ajaxify.data.matchCount]);
    		} else {
    			_paq.push(['trackPageView']);
    		}
            _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);
    
        // added this line
        firePiwik(ajaxify.data);
    
        $(window).on('action:ajaxify.end', function(ev, data) {
            firePiwik(data);
        });
    });
    </script>
    
    rodR 1 Reply Last reply
    0
  • rodR Offline
    rodR Offline
    rod
    replied to <baris> on last edited by
    #17

    @baris I cannot get this to work. My new plan is to destroy my 1.12.1 version and install 1.6.x and test. If that works do the same going to version 1.7.x and test, and keep jumping forward until it doesn’t work and then report back.

    Thank you for all of your help thus far.

    rodR 1 Reply Last reply
    1
  • rodR Offline
    rodR Offline
    rod
    replied to rod on last edited by
    #18

    @baris. I reverted my dev site back to my production '1.4.5' version and it logged properly to Piwik (Matomo.). I upgraded to v1.6.1 and that version worked fine also, reporting to Piwik.

    I upgrade to v1.7.5 and voila reporting to Piwik no longer works. The 1.7.x branch is the one in which the Custom Javascript was broken out from Custom Header. I truly believe this is the root of the problem.

    Has anyone else reported oddities with custom javascript after the introduction of the 1.7 branch?

    Thank you.

    1 Reply Last reply
    0
  • barisB Offline
    barisB Offline
    <baris> NodeBB
    wrote on last edited by
    #19

    We got a few reports of action:ajaxify.end not triggering since the scripts were moved to the footer but those are fixed by the code sample I showed.

    Maybe it has to do with the way the piwik script is being loaded. Can you try this in custom header instead.

    <script type="text/javascript" src="//your.piwikurlhere.com/piwik.js"></script>
    <script>
    window.addEventListener('load', function () {
        console.log(window.hasOwnProperty('_paq'))
        var _paq = _paq || [];
    
        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 === 'search') {
    			_paq.push(['trackSiteSearch', ajaxify.data.search_query,, ajaxify.data.matchCount]);
    		} else {
    			_paq.push(['trackPageView']);
    		}
            _paq.push(['enableLinkTracking']);
            _paq.push(['setTrackerUrl', u+'piwik.php']);
            _paq.push(['setSiteId', 1]);
        }
    
        // added this line
        firePiwik(ajaxify.data);
    
        $(window).on('action:ajaxify.end', function(ev, data) {
            firePiwik(data);
        });
    });
    </script>
    

    Then check browser console to make sure piwik is properly loaded.

    rodR 1 Reply Last reply
    0
  • rodR Offline
    rodR Offline
    rod
    replied to <baris> on last edited by
    #20

    @baris Still no success. I am watching the web server logs of the Piwik server and I see a hit for the loading of "/piwik.js" (as I should as you call the URL in your first script tag but I don't see any further web server entries after that.

    I switch over to the my production NodeBB forum and while watching the Piwik web server log files I see all of the action I expect to see.

    Is there any sort of special widget or plugin I could install in which I could put this javascript and have it run as it should. I wonder what in NodeBB is stomping on it?

    I do see true in my browser console, from console.log(window.hasOwnProperty('_paq'))

    1 Reply Last reply
    0
  • barisB Offline
    barisB Offline
    <baris> NodeBB
    wrote on last edited by
    #21

    @baris said in In which version did Custom Javascript show up?:

    _paq.push(['setTrackerUrl', u+'piwik.php']);

    Did you fix this to be _paq.push(['setTrackerUrl', '//your.piwikurlhere.com/piwik.php']); as well?

    If it doesn't work in custom header/javascript then I don't think it's gonna work in a widget either. You can try putting the same code in a global sidebar widget and test it.

    rodR 1 Reply Last reply
    0
  • rodR Offline
    rodR Offline
    rod
    replied to <baris> on last edited by
    #22

    @baris Thanks for all of the pointers. The javascript does not seem to work in a Widget, as you deduced, either.

    I'm at a loss as I'm not a JS programmer. I'll just stick with v1.4.5 for a while longer for my production forum. Possibly I will figure something else out.

    1 Reply Last reply
    0
  • rodR Offline
    rodR Offline
    rod
    wrote on last edited by
    #23

    Well I am embarrassed, I figured out the problem! There is no problem with custom javascript or the custom header information. It turns out the nodebb-plugin-ns-twitch-monitor was the problem! The plugin does not seem to work after a certain version of NodeBB and where I had it placed in my widgets must have been causing the custom header code to not load or run properly.

    I am still running v1.4.5 but am moving over to a new server, yes after all of these years. In doing so I have been incrementally stepping through upgrades, 1.4.5 -> 1.5.x -> 1.6.x -> 1.7.x... It was there at the 1.7.x that my custom Piwik (Matomo) code stopped working. I put NodeBB into "dev" mode and saw a number of errors related to the Twitch plugin. I removed the plugin and my analytics code now works fine.

    It is bittersweet as I really like the Twitch plugin on my forum but the plugin stopped working quite some time ago. Twitch changed how their API works and the plugin was never updated to keep up.

    Shouting out to a list of people for closure on this, in case you were all losing sleep on this since May of last year! @baris @julian @Nicolas

    1 Reply Last reply
    1

Copyright © 2023 NodeBB | Contributors
  • Login

  • Don't have an account? Register

  • Login or register to search.
Powered by NodeBB Contributors
  • First post
    Last post
0
  • Home
  • Categories
  • Recent
  • Popular
  • Top
  • Tags
  • Users
  • Groups
  • Documentation
    • Home
    • Read API
    • Write API
    • Plugin Development