Welcome to TalkGraphics.com
Results 1 to 5 of 5
  1. #1

    Default Matomo integration - how I track clicks

    I'm not sure if anyone has used Matomo for analytics, but I've had some difficulties getting some of the events to work. Specifically I have pages with a FAQ/Answer layout with a bunch of questions at the top, which link to answers further down the page. I've set it up this way so I can track which questions get clicked on more.

    One of the issues with Matomo is that it doesn't track links within the same page automatically, as far as I can tell. Hopefully there's an easier way, but I ended up creating a script that sends data to matomo when the link is clicked. There were a few issues as the script needs to run after the document is loaded, but after all of the Xara code. Anyway, in case it helps someone else here is the script I used:

    Code:
    <script>
    
    function AddClicks() {
    	var l = document.links;
    	for(var i=0; i<l.length; i++) {	
    		l[i].addEventListener('click', MyClick,false); 
    	}
    }
    
    function AddClicksInit() {
    	setTimeout(function() { AddClicks(); }, 500);
    }
    	
    function MyClick(event)
    {
    	if (typeof(event) == "undefined") { return;}
    	var target = ''+event.target;
    	var name = target.substr(target.lastIndexOf('/')+1);
    	_paq.push(['trackEvent', 'DynamicClicks', 'Click',name]);
    }
    
    window.addEventListener("load", AddClicksInit);
    window.addEventListener("resize", AddClicks);
    </script>
    In my case it automatically names the clicks based on the name of the page and the xara name/anchor. Note that the names are different for the different variants, and if you have pages with the same name but located in different folders that could be an issue. It need to be in the <body> section of the website.

    Anyway, hope that helps someone.

  2. #2

    Default Re: Matomo integration - how I track clicks

    Made a few improvements:

    Code:
    <script>
    
    function AddClicks() {
    	var l = document.links;
    	for(var i=0; i<l.length; i++) {	
    		l[i].addEventListener('click', MyClick,false); 
    	}
    }
    
    function MyClick(event)
    {
    	if (typeof(event) == "undefined") { return;}
    	var target = ''+event.currentTarget.href;
    	var name = target.substr(target.lastIndexOf('/')+1);
            _paq.push(['trackEvent', 'DynamicClicks', 'Click',name]);
    }
    
    window.addEventListener("load", AddClicks);
    window.addEventListener("resize", AddClicks);
    </script>

  3. #3
    Join Date
    Apr 2012
    Location
    SW England
    Posts
    17,921

    Default Re: Matomo integration - how I track clicks

    paco, I do not see why you need to re-apply the AddClicks function on window resize. Onload will have done it already.
    All Variant links are being caught too.

    All the code will sit nicely in Website > HTML Code (body).

    Your event listener for resize, if needed, is hijacking the current Windows set up.
    Have you tried window.onresize = function() { AddClicks }; ?

    If you change the function AddClicks to run immediately, you don't need the onload part either:

    (function(){
    var l = document.links;
    for(var i=0; i<l.length; i++) {
    l[i].addEventListener('click', MyClick,false);
    }
    }())

    None of the above are tested, just ideas.

    Acorn
    Acorn - installed Xara software: Cloud+/Pro+ and most others back through time (to CC's Artworks). Contact for technical remediation/consultancy for your web designs.
    When we provide assistance, your responses are valuable as they benefit the community. TG Nuggets you might like. Report faults: Xara Cloud+/Pro+/Magix Legacy; Xara KB & Chat

  4. #4

    Default Re: Matomo integration - how I track clicks

    Quote Originally Posted by Acorn View Post
    paco, I do not see why you need to re-apply the AddClicks function on window resize. Onload will have done it already.
    All Variant links are being caught too.

    All the code will sit nicely in Website > HTML Code (body).

    Your event listener for resize, if needed, is hijacking the current Windows set up.
    Have you tried window.onresize = function() { AddClicks }; ?

    If you change the function AddClicks to run immediately, you don't need the onload part either:

    (function(){
    var l = document.links;
    for(var i=0; i<l.length; i++) {
    l[i].addEventListener('click', MyClick,false);
    }
    }())

    None of the above are tested, just ideas.

    Acorn
    Thanks for the suggestions Acorn. I have tested it and find that if you resize a desktop window down to a mobile size, all of the event listeners are lost - I assume that xara does that when changing variants - so the resize listener is needed so that they are reimplemented.

    Agreed about putting it in the body and removing the AddClicksInit - that works fine. I initially had all the code set up as a placeholder so it was further up in the body, and thus would run before the xara code at the bottom. As you say, puttting in the website body function inserts it at the end so this is not needed.

    Great discussion, thanks!

  5. #5
    Join Date
    Apr 2012
    Location
    SW England
    Posts
    17,921

    Default Re: Matomo integration - how I track clicks

    Paco, useful as I never thought of using Matomo.
    I'll give it a try.

    Acorn
    Acorn - installed Xara software: Cloud+/Pro+ and most others back through time (to CC's Artworks). Contact for technical remediation/consultancy for your web designs.
    When we provide assistance, your responses are valuable as they benefit the community. TG Nuggets you might like. Report faults: Xara Cloud+/Pro+/Magix Legacy; Xara KB & Chat

 

 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •