Confusion about _paq.push and _mtm.push

Hi there!
I’m tracking a website in matomo. According to the doc, i need to paste the following code in the block

<!-- Matomo -->
<script>
  var _paq = window._paq = window._paq || [];
  /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="//xxx:xxx/";
    _paq.push(['setTrackerUrl', u+'matomo.php']);
    _paq.push(['setSiteId', '5']);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
  })();
</script>
<!-- End Matomo Code -->

But my site is based on SPA. I can’t track the url change using the code upside. So I add a tag manager according to this doc(https://developer.matomo.org/guides/spa-tracking). And I have to paste another code in my block

<!-- Matomo Tag Manager -->
<script>
  var _mtm = window._mtm = window._mtm || [];
  _mtm.push({'mtm.startTime': (new Date().getTime()), 'event': 'mtm.Start'});
  (function() {
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.async=true; g.src='http://x:9530/js/container_t5hH6kJg.js'; s.parentNode.insertBefore(g,s);
  })();
</script>
<!-- End Matomo Tag Manager -->

I’m confused about the _paq.push and _mtm.push methods. Do I need to keep both codes in my tags? What’s the difference between the _paq and _mtm?

Hi @CongHuiJia
_paq is used for low level JavaScript API, whereas _mtm the high level Tag Manager API. They don’t work very well together, then I suggest you use one (the one you prefer) but not the other…
Note: you can also do SPA tracking with _paq:
https://developer.matomo.org/guides/spa-tracking#solution-2-embedding-the-tracking-code-manually
The use of _mtm is more controllable server-side and probably easier to use, but _paq can manage visit reset or referrer change, that _mtm cannot.

Hi Philippe,
Thanks for answering. I choose _mtm for my situation. But there is a new problem: I followed solution 1 in this doc (https://developer.matomo.org/guides/spa-tracking). It works well on tracking the url change. But it can not track the page title. What should I do to send the title to my matomo server?

Hi @CongHuiJia
You can configure an MTM variable (JavaScript variable or DOM Element variable or Data-Layer variable or…).
Then just use this variable to set the Custom Title in the Matomo Analytics tag used for page view.

Hi Philippe,
I tried the method you just said. But a problem came again. There is a title on the browser represents website title. When page tab is clicked, there is a title represents page title. Since my website is based on SPA, I can only get the page title by refreshing the page once in js code, which will cause duplicate report to matomo server. Are there any methods to solve this problem?

Hi @CongHuiJia
There is something somewhere in your code that simulates a page change. This is the place where you probably trigger the Page View tag. And this is the place where you have to decide the page title you want to give to your Page View.

Thanks, I’ll give a try