Jump to content

Javascript Timing


Recommended Posts

DILEMMA:  Using Javascript with Matomo to track events is somewhat elusive.  This is because the Javascript that communicates with the Matomo application is generally placed on a visitor-friendly, visible HTML document, while the events that one wishes to track are often triggered by Javascript after the page has loaded.

QUESTION: In light of the above,. when Javascript is called after a page has loaded is the Javascript that loaded with the page called again? Or, is it only the Javascript that was called after the page has been loaded?


Link to comment
Share on other sites

Is it possible for the Matomo application to trigger the Javascript?

Let me provide a more concrete example of what I mean.

On the Grammar Captive mainpage is a standard Matomo tracking script that appears as follows:

<script type="text/javascript">
	var _paq = _paq || [];
	(function() {
		var u="//.../matomo/";
		_paq.push(['setTrackerUrl', u+'piwik.php']);
		_paq.push(['setSiteId', '1']);
		var d=document, 

Indeed, the above script is on every webpage whose visitor behavior I wish to track.  On the mainpage in particular various events are triggered using Javascript after the page has loaded.  Each of these events includes a piece of code similar to the following:

_paq.push(['trackEvent', 'Gate One', 'Click', fileName]);   				

The consists of the pushed array element consist of a Matomo method called trackEvent().  Associated with each such event are three parameters used to identify the event. 

Now, with the exception of the four standard Matomo methods that fill the _paq array when the page is first loaded, every additional event triggered by Javascript and containing a _paq.push({...}); statement occurs after the page has been loaded.  Does this imply that these latter events will not be properly tracked unless I reload the first script with each and every new triggered event?



Link to comment
Share on other sites

I couldn't answer that without looking at the code which uses that array, but I imagine that the array is not constantly polled to look for new additions.  In that case the array would have to be populated prior to including the other script.

With modern Javascript it should be possible to use a Proxy to respond to updates on something like an array but, again, whatever the code using that array does cannot be determined without looking at the actual code.  Reading through the actual code is typically the last word for these kinds of questions.

  • Thanks 1
Link to comment
Share on other sites

It turns out that Matomo updates the tracking script on a a regular basis, else it could not be recording the events that it has recorded in the absence of repeated use of the tracking script.  Thank you for making it clear that this option exists.


Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...