Tracking Code for Using Multiple Piwik Trackers


I’ve checked out JavaScript Tracking Client: API Reference - Matomo Analytics (formerly Piwik Analytics) - Developer Docs - v3 and wanted to clarify ‘exactly’ how my code should look to ensure both of the trackers are set up correctly.

Do I only need to use this code:

<script type="text/javascript">

window.piwikAsyncInit = function () {
    try {
        var piwikTracker = Piwik.getTracker("http://subdomain.URL_1/", 1);
        var piwik2 = Piwik.getTracker("http://subdomain.URL_2/", 4);
    } catch( err ) {}


or do I need to include this somehow within the javascript tracking code that Piwik generates via Settings/tracking area?

thanks for any help solving this.



Please answer!

@Paul your code looks good!