Mautic cookies: mtc_id mautic_session_id mautic_session_name

hi folks,



I’m trying to understand cookie behavior in mautic and at first sight it wasn’t obvious. I understand there are a few cookies, namely:



mtc_id: id of the user

mautic_session_id: session id, and there is another cookie with this id that points to the user id

mautic_session_name: I don’t know what this is used for



The problem is that mtc_id seems to be sometimes set and sometimes not (at least in my case, I have a suspicion using https for the tracking js is making things a bit more challenging).



Question is: how can I get the id of the user (reliably) without an API call, just using cookies assuming CORS and cookie domain settings are fine. Shall I do an OR with $_COOKIE[“mtc_id”] or $_COOKIE[$_COOKIE[“mautic_session_id”]]?







Below is my search on the topic if anyone ends up here from a search.



There is a very old thread at: https://www.mautic.org/community/index.php/82-how-can-i-use-the-mautic-api/0#p536 on the topic but it’s so old I can’t rely on it.



Also



https://developer.mautic.org/#manipulating-contacts-(leads)



has some php code


<?php
// Currently tracked lead based on cookies
$leadModel = $this->getModel('lead');
$lead = $leadModel->getCurrentLead();
$leadId = $lead->getId();

Which I'm not sure about because I can't figure out what it does.

Any suggestions?

hi folks,

I’m trying to understand cookie behavior in mautic and at first sight it wasn’t obvious. I understand there are a few cookies, namely:

mtc_id: id of the user
mautic_session_id: session id, and there is another cookie with this id that points to the user id
mautic_session_name: I don’t know what this is used for

The problem is that mtc_id seems to be sometimes set and sometimes not (at least in my case, I have a suspicion using https for the tracking js is making things a bit more challenging).

Question is: how can I get the id of the user (reliably) without an API call, just using cookies assuming CORS and cookie domain settings are fine. Shall I do an OR with $_COOKIE[“mtc_id”] or $_COOKIE[$_COOKIE[“mautic_session_id”]]?

Below is my search on the topic if anyone ends up here from a search.

There is a very old thread at: https://www.mautic.org/community/index.php/82-how-can-i-use-the-mautic-api/0#p536 on the topic but it’s so old I can’t rely on it.

Also

https://developer.mautic.org/#manipulating-contacts-(leads)

has some php code

<?php // Currently tracked lead based on cookies $leadModel = $this->getModel('lead'); $lead = $leadModel->getCurrentLead(); $leadId = $lead->getId(); Which I'm not sure about because I can't figure out what it does. Any suggestions?

Are you working on a local installation? I also for the past year have been finding the mtc_id to be extremely unreliable; however, it’s mostly while working on my local installation.

I just tracked down what looks like a change a year ago:
https://github.com/mautic/mautic/commit/90b64034d09510d6f77fb9e790e91938bbadec9b#diff-680a217018ae349738fa38e34b7a19f8R152

Where 127.0.0.1 is always added to do not track. This means you’ll get a mautic_session_id, but not a mtc_id.

Later on it was added so this functionality only happens in production:
https://github.com/mautic/mautic/commit/78a64d35677b9c19fcbeacf3f1d4879e4522a208

However, I didn’t realized this so I never thought to put my dev url into my local website tracker
i.e. instead of tracking https://mautic.example.org/mtc.js use https://mautic.example.org/index_dev.php/mtc.js

That was my issue at least.