In order to set the User ID using Matomo Tag Manager, we will need to pass the User ID from our website to the Matomo Tag Manager data layer.

Before we begin, you need to have some way to assign the User ID to a variable in your website.
Once you have this setup, then you can proceed with the steps below.

Setting up User ID using the Matomo Tag Manager:

1) First we need to declare the Matomo Tag Manager data layer variable in our website with the following:

var _mtm = _mtm || []; 

2) Next we need to push the User ID that we gather from our website to the MTM data layer:

_mtm.push({'uid':'unique_user_id_here' });
// Replacing unique_user_id_here with the variable that you define in your website code for the User ID

For more information on generating a User ID please see our guide here: User ID

3) Next we will create a new datalayer variable in the Matomo Tag Manager

Go to Tag Manager > Select the Website you wish to create the variable for > Select “Variables” in the lower left menu > click “Create New Variable” > and select “Data-Layer”

Next, give it the name uid and a Data Layer Variable Name uid

And click “Create New Variable”

4) Next we will use this new data layer variable to set the User ID in your Matomo Configuration variable in Matomo Tag Manager

Click on “Variables” in the left menu and find your main “Matomo Configuration” variable in the list and click on the “Edit” button.
Scroll down and click the “Choose a variable” button and select the uid (Type: DataLayer) variable from the list

5) Verify that User ID is tracked

Once you’ve setup the uid variable using the steps above, publish the changes to your MTM container.

Then, go to your website while logged in to generate a tracking request
(You may need to refresh the page or clear your browser cache to ensure that the new MTM container is loaded, and ensure that your traffic is not excluded from tracking)

Then go to Matomo, open “Visitors > Visits log”, select Today in the calendar, and check that your visit was tracked with the User ID

You are now successfully tracking User IDs in Matomo using the Matomo Tag Manager and data layer!!

For more information on setting up Matomo Tag Manager in your website, see our Matomo Tag Manager guide.

Previous FAQ: Set up the Matomo Configuration Variable