Wrapping JavaScript tracking code

I have following tracking code.


<!-- Tracking Code --> 
<script type="text/javascript">
var pkBaseURL = (("https:" == document.location.protocol) ? "https://example.com/" : "http://example.com/");
document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
</script><script type="text/javascript">
try {
  var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 29);
  piwikTracker.trackPageView();
  piwikTracker.enableLinkTracking();
} catch( err ) {}
</script><noscript><p><img src="http://example.com/piwik.php?idsite=29" style="border:0" alt="" /></p></noscript>
<!-- Tracking Code -->

I want to hide following code, so I think I can put this code in some js file, and load it using following code.

But I don’t know how can I do it. Help required here.

Secondly, I need to change site id (in above example, is 29), which will be different for different site.

How can I pass site-id to custom file that I will make, and execute above code.

Is this Document how to hide piwik server URL in the Javascript · Issue #2019 · matomo-org/matomo · GitHub what you’re looking for or do you simply want to include a javascript file instead of inserting a markup in your page ?