Testing Matomo Implementations within GTM
When debugging Matomo (formerly PiWik) with Google Tag Manager (GTM), you may have noticed that the Matomo scripts execute and send data to your Matomo server. This is because while GTM is designed not to send data for Google Analytics tags, all JavaScript or other code in HTML tags do execute. The Matomo code then fires and sends its data to the servers as it fires within the HTML tag.
(Read our earlier post to learn how to use GTM for Matomo</A> tagging.)
With a Matomo Site ID Constant Variable
- Create a Matomo Site ID for development purposes
- Create a GTM constant variable for the Matomo Site ID
- Use the Matomo Site ID Constant Variable within the Matomo tracking script such as
_paq.push(['setSiteId', '{{Matomo SiteID}}']);
- Complete your testing.
- When done, change the Matomo Site ID to your production report Site ID and PUBLISH.
Dynamically with a Lookup Table
You can also use a dynamic setting based on the use of the PREVIEW mode. When GTM PREVIEW is in use, activate GTM in its DEBUG mode. You can check if this is active and, if so, use the development Site ID and, if not, use the production Site ID.
- Create a Matomo Site ID for development purposes.
- Activate the GTM DEBUG variable.
- Create a Look Up table with the input value as the DEBUG variable.
- Place two lines in the Lookup Table: true and false.
- For the value of true (DEBUG is on), place in your development Matomo Site ID. This could also be a constant variable if you use this Site ID elsewhere.
- For the value of false (DEBUG is on), place in your production Matomo Site ID. This could also be a constant variable if you use this Site ID elsewhere.
- We set the Default Value to your production Matomo Site ID. This is not actually necessary, but we have seen UNDEFINED for the debug variable. This is there as a safety point to ensure data is tracked even if the DEBUG MODE variable has an error.
- Use the Matomo Site ID Lookup Table Variable within the Matomo tracking script such as:
_paq.push(['setSiteId', '{{Matomo Site ID Lookup}}']);
- You should now test in both Preview and Published mode that the proper Site ID is used. You will now have a method that automictically directs traffic based on the PREVIEW setting for any user.