Custom event trigger with _mtm doesn't work

Hello,

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'});
_mtm.push({'event':'js_alert'});
</script>

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.

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.

Hi,

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.

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.

Hi,

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

1 Like

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.

Le debug

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

Hi,

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

No, because the guide says it must be before the container (at the bottom of this page https://developer.matomo.org/guides/tagmanager/datalayer)

just tried and it didn’t worked.

ok je commence à tester et te tient au courant

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.

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

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.

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

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

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

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:

1 Like

Bonjour,
J’ai le même problème. Je n’arrive pas à déclencher un évènement même un simple alert().
On dirait qu’aucun de mes évènements personnalisé n’est pris en compte.
J’y ai passé la journée je sèche. Pourtant si teste le même code avec GTM ça fonctionne.
Je suis sûrement passé à côté de quelquechose.
Merci pour votre aide.

Hi,

The initial post is 4 years old, and so the solution…

It should have been better to start a new post…

Did you publish your container?
Did you test it in debug mode? Are the trigger… triggered?
Is there any JavaScript error?
Note: Use of console.log() is better than alert()