Skip to content
  • Recent
  • Tags
  • Popular
  • Users
  • Search
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

Lay Theme Forum

  1. Home
  2. General Discussion
  3. How to setup Matomo (formerly Piwik) to track page views correctly.

How to setup Matomo (formerly Piwik) to track page views correctly.

Scheduled Pinned Locked Moved General Discussion
2 Posts 2 Posters 140 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.
  • N Offline
    N Offline
    neunzehnachtneun
    wrote on last edited by
    #1

    Hi all!
    I am using matomo together with laytheme. And as you know, laytheme builds javascript single page application, the page visits are hardly to track. Luckily after a long search I found a code snippet, that helped to get this functionality and I want to share it to those, who are struggling also. Inserting it into the head section will get the page view tracking running.

    Cheers!
    Sebastian

    <!-- Matomo -->
    <script type="text/javascript">
    
      // Custom locationchange event
      history.pushState = ( f => function pushState(){
          var ret = f.apply(this, arguments);
          window.dispatchEvent(new Event('pushstate'));
          window.dispatchEvent(new Event('locationchange'));
          return ret;
      })(history.pushState);
    
      history.replaceState = ( f => function replaceState(){
          var ret = f.apply(this, arguments);
          window.dispatchEvent(new Event('replacestate'));
          window.dispatchEvent(new Event('locationchange'));
          return ret;
      })(history.replaceState);
    
      window.addEventListener('popstate',()=>{
        window.dispatchEvent(new Event('locationchange'))
      });
      
      var _paq = window._paq || [];
      _paq.push(['trackPageView']);
      _paq.push(['enableLinkTracking']);
    
      (function() {
        var u="//{$PIWIK_URL}/";
        _paq.push(['setTrackerUrl', u+'matomo.php']);
        _paq.push(['setSiteId', {$IDSITE}]);
        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);
      })();
      
      // Single Page Application Tracking
      var formerUrl = location.href;
      window.addEventListener('locationchange', function() {
          _paq.push(['setReferrerUrl', formerUrl]);
          _paq.push(['setCustomUrl', location.href]);
          //_paq.push(['setDocumentTitle', document.title]);
    
          // remove all previously assigned custom variables, requires Matomo (formerly Piwik) 3.0.2
          _paq.push(['deleteCustomVariables', 'page']); 
          _paq.push(['setGenerationTimeMs', 0]);
          _paq.push(['trackPageView']);
      });
    </script>
    <!-- End Matomo Code -->
    
    1 Reply Last reply
    2
    • mariusjopenM Offline
      mariusjopenM Offline
      mariusjopen
      Global Moderator
      wrote on last edited by
      #2

      Dear @neunzehnachtneun
      thank you @neunzehnachtneun for sharing!

      We see how we can use this in the future.

      Best!

      Marius

      www.mariusjopen.world

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      I also code custom websites or custom Lay features.
      šŸ’æ Email me here: šŸ’æ
      info@laytheme.com

      Before you post:
      1. When using a WordPress Cache plugin, disable it or clear your cache.
      2. Update Lay Theme and all Lay Theme Addons
      3. Disable all Plugins
      4. Go to Lay Options → Custom CSS & HTML, click "Turn Off All Custom Code", click "Save Changes"

      This often solves issues you might run into

      When you post:
      1. Post a link to where the problem is
      2. Does the problem happen on Chrome, Firefox, Safari or iPhone or Android?
      3. If the problem is difficult to explain, post screenshots / link to a video to explain it
      Online Users
      Forgot your key, lost your files, need a previous Lay Theme or Addon version? Go to www.laykeymanager.com
      laytheme.com
      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Recent
      • Tags
      • Popular
      • Users
      • Search