Custom event trigger with _mtm doesn't work

(Cédric) #1


i need to trigger a tag with a custom event. For test, i have created a test tag with a simple javascript alert. And i have placed this code before the container between but it doesn’t work:

<script type="text/javascript">
var _mtm = _mtm || [];
_mtm.push({'mtm.startTime': (new Date().getTime()), 'event': 'mtm.Start'});

note: The tag is triggered if i replace the trigger “custom event” for the predefined “pageview”.

I don’t know what i am doing wrong. Please, can you help me?
Thank you very much.

(Ronan Chardonneau) #2

Hi Cédric,

Thanks for your post, I wish I could see more post here about Matomo Tag Manager.
I am a bit confused, I am not sure about what you are trying to achieve.
Could you please tell me what you would like to get at the end?
I mean do you just want to get a JavaScript alert to fire when a page is viewed? I mean this is what I understood.

Just confirm this point and I will do my best to answer accordingly.

(Cédric) #3


I just want to trigger the tag with a custom event. it seems that the _mtm var is not read by the container script. i don’t see anything relative to the _mtm var in the debug window.

Sorry for my bad english. I’m french.

(Ronan Chardonneau) #4

Sure, no problem Cédric, do not hesitate to post within the French forum, I will answer in French on it. Do you have a custom event in mind you would like to use as a trigger? By default MTM is offering many.

(Lukas Winkler) #5


I have moved this into the French forum so you can continue here.

(Cédric) #6

Ok merci :slight_smile:

Voilà. J’ai donc créé un trigger personnalisé (voir les screenshoots) pour qu’il déclenche un tag. Mais le tag ne se déclenche pas.

Lorsque je change le déclencheur du tag par un simple “pageview”, le tag se déclenche.

Lorsque je regarde dans la fenetre de debug, je n’ai aucune des variables présentes dans _mtm.

(Cédric) #7

Le debug

J’ai fait volontairement un tag simple avec la fonction alert() pour tester.

(Lukas Winkler) #8


Simple idea: Have you tried adding your code after the container?

(Cédric) #9

No, because the guide says it must be before the container (at the bottom of this page Data Layer: Integrate - Matomo Analytics (formerly Piwik Analytics) - Developer Docs - v3)

(Cédric) #10

just tried and it didn’t worked.

(Ronan Chardonneau) #11

ok je commence à tester et te tient au courant

(Cédric) #12

je viens de tester avec une page beaucoup plus simple. Même debug, même problème. Je pense qu’il doit me manquer un prérequis ou alors c’est un bug.

<script type="text/javascript">
var _mtm = _mtm || [];
_mtm.push({'mtm.startTime': (new Date().getTime()), 'event': 'mtm.Start'});
<script type="text/javascript" src="http://localhost/Matomo/js/container_7n4zJX0t.js" async="true" defer="true"></script>

(Ronan Chardonneau) #13

en effet, je n’arrive pas à comprendre pourquoi cela ne marche pas non plus de mon côté, j’ai bien le datalayer avec l’evenement jsalert dans la console mais cela ne se déclenche pas.

(Ronan Chardonneau) #14

Je viens de pinger le créateur du plugin je te fais signe dès que j’ai une réponse.

(Cédric) #15

D’accord. Merci beaucoup!


That’s a bug and I guess the docs are not 100% clear there. Will change them.

What would work is currently triggering the event after the tag manager has loaded, not before. Simple test that shows this could be

setTimeout(function () { _mtm.push({'event':'js_alert'}); }, 5000);

Which triggers the event after 5s. I’ve tried to create a quick fix for this but it is not trivial to make this happen and need to spend a day or so checking how we can make this work.

Meanwhile, I suggest you follow First init triggers, then the dataLayer by tsteur · Pull Request #32 · matomo-org/tag-manager · GitHub

(Cédric) #17

Hello thomas,
I see you have fixed this on github.
Thank you for your work.

(Ronan Chardonneau) #18

et ça marche !!! je viens tout juste de tester :slight_smile: merci Cédric d’avoir trouvé ce bug et merci Thomas de l’avoir corrigé :slight_smile: