Load matomo tracking with a javascript function

Load the matomo tracking code with a javascript function don’t set cookies.

The matomo tracking should be loaded, when a function is called. The source script loaded in the works. The _paq array is filled. The function is called. But cookies don’t saved.
PS: with Google Analytics it works.

var functions_object = {};

functions_object[“matomo”] = function () {
var _paq = _paq || [];
/* tracker methods like “setCustomDimension” should be called before “trackPageView” */
_paq.push([‘trackPageView’]);
_paq.push([‘enableLinkTracking’]);
(function() {
var u="//www.example.com/matomo/";
_paq.push([‘setTrackerUrl’, u+‘matomo.php’]);
_paq.push([‘setSiteId’, ‘99’]);
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+‘matomo.js’; s.parentNode.insertBefore(g,s);
})();
};
The complete script is embedded in the (>1000 rows).

I call the functions in the functions_object inside another function with a for loop. This works fine by all, except matomo.

comment out the lines (function() { and })(); have no effect.

The matomo code works outside the function, also blank in the script, cookies are saved.