# User\_id tracking implementation

**URL:** <https://community.piwik.pro/t/user-id-tracking-implementation/1886>\
**Category:** Data collection and tags\
**Created:** [March 24, 2023, 8:19am UTC](https://community.piwik.pro/t/user-id-tracking-implementation/1886 "2023-03-24T08:19:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ValPiwik](https://avatars.discourse-cdn.com/v4/letter/v/ecd19e/32.png) [@ValPiwik](https://community.piwik.pro/u/ValPiwik)\
**Post date:** [March 24, 2023, 8:19am UTC](https://community.piwik.pro/t/user-id-tracking-implementation/1886/1 "2023-03-24T08:19:15Z")

</div>

Hello all,

To track the user\_id, what is your recommendation?

1. Trigger only the datalayer when the user logged in
2. Trigger the datalayer on every page where the user is logged in?

In the case of the solution 2: is possible to have a double counting issue?

Thks 🙂

---

<div class="post-metadata">

**Author:** ![Oliwer\_Kaczmarek](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/oliwer_kaczmarek/32/3888_2.png) [@Oliwer\_Kaczmarek](https://community.piwik.pro/u/Oliwer_Kaczmarek)\
**Post date:** [March 24, 2023, 10:46am UTC](https://community.piwik.pro/t/user-id-tracking-implementation/1886/2 "2023-03-24T10:46:23Z")

</div>

Hi,

Both solutions are perfectly okay. The difference is that in the case of the first one, you need to write a short script. While in the case of the second one, you can use the toggle “Set a user ID” in the Piwik PRO tag and then put in the variable with the user ID.  
Here is article: [Set up a user ID | Piwik PRO help center](https://help.piwik.pro/support/getting-started/userid/)

Whenever you send a user ID during one session, the previous value gets overwritten by the new one. There should not be an issue with double counting.

Best,  
Oliwer

---

<div class="post-metadata">

**Author:** ![ValPiwik](https://avatars.discourse-cdn.com/v4/letter/v/ecd19e/32.png) [@ValPiwik](https://community.piwik.pro/u/ValPiwik)\
**Post date:** [March 24, 2023, 2:49pm UTC](https://community.piwik.pro/t/user-id-tracking-implementation/1886/3 "2023-03-24T14:49:50Z")

</div>

Hi @Oliwer_Kaczmarek ,

Thank you for the answer.

We chose the scenario 2.  
We implemented it for GA4 through Piwik PRO TMS, not yet for Piwik but the process is similar.

The weird thing is that we have a double counting in GA4, so I would avoid the same story in Piwik…

```auto
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>

<script>
 window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-XXXXXXXXXX', { 
    'page_location': location.hostname + location.pathname + location.hash 
  }
      );

  // set user_id and user properties
  gtag('set', {'user_id': {{ DLV | User ID }}});
  gtag('set', {'user_properties': {
    'city': {{ DLV | City }},
     'country': {{ DLV | Country }},
    'language': {{ DLV | Language }},
  }});
  
</script>

```

Do you see something weird in this code that could explain the double counting?  
I assume that the same code adapted for Piwik will have the same issue…

Many thanks,

---

<div class="post-metadata">

**Author:** ![Oliwer\_Kaczmarek](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/oliwer_kaczmarek/32/3888_2.png) [@Oliwer\_Kaczmarek](https://community.piwik.pro/u/Oliwer_Kaczmarek)\
**Post date:** [March 27, 2023, 8:22am UTC](https://community.piwik.pro/t/user-id-tracking-implementation/1886/4 "2023-03-27T08:22:39Z")

</div>

Hi,  
Not sure why GA4 is double counting data as it’s not the tool I’m familiar with. The script doesn’t appear to have any visible bugs. When it comes to Piwik PRO, there should not be a problem with double counting.

Best,  
Oliwer

---

<div class="post-metadata">

**Author:** ![ValPiwik](https://avatars.discourse-cdn.com/v4/letter/v/ecd19e/32.png) [@ValPiwik](https://community.piwik.pro/u/ValPiwik)\
**Post date:** [March 27, 2023, 10:14am UTC](https://community.piwik.pro/t/user-id-tracking-implementation/1886/5 "2023-03-27T10:14:37Z")

</div>

> [@Oliwer\_Kaczmarek](#):
>
> ear to have any visible bugs. When it comes to Piwik PRO, there should not be a problem with double counting.

Thank you for your answer 🙂 It confirms me in the fact to have chosen the good scenario

---

<div class="post-metadata">

**Author:** ![ValPiwik](https://avatars.discourse-cdn.com/v4/letter/v/ecd19e/32.png) [@ValPiwik](https://community.piwik.pro/u/ValPiwik)\
**Post date:** [March 28, 2023, 8:19am UTC](https://community.piwik.pro/t/user-id-tracking-implementation/1886/6 "2023-03-28T08:19:36Z")

</div>

@Oliwer_Kaczmarek ,

Is it possible that Piwik trigger the tag twice in asynchronous way?  
First the TMS load the GA4 tag, once is done, it sents a kind of notification to Piwik to trigger the user\_id and user\_properties?  
So the tag is triggered twice

I see two requests in the dev tool:

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/piwikpro/original/2X/f/fb426432861ce736c38f0141e9970b458bd5eae2.png)

---

<div class="post-metadata">

**Author:** ![Oliwer\_Kaczmarek](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/oliwer_kaczmarek/32/3888_2.png) [@Oliwer\_Kaczmarek](https://community.piwik.pro/u/Oliwer_Kaczmarek)\
**Post date:** [March 28, 2023, 11:06am UTC](https://community.piwik.pro/t/user-id-tracking-implementation/1886/7 "2023-03-28T11:06:44Z")

</div>

@ValPiwik

In debug mode you can see how many times a tag is triggered on a page and what datalayer events went through. If it shows that the tag was fired multiple times than there might be an issue with the trigger.

---

<div class="post-metadata">

**Author:** ![ValPiwik](https://avatars.discourse-cdn.com/v4/letter/v/ecd19e/32.png) [@ValPiwik](https://community.piwik.pro/u/ValPiwik)\
**Post date:** [March 28, 2023, 11:28am UTC](https://community.piwik.pro/t/user-id-tracking-implementation/1886/8 "2023-03-28T11:28:23Z")

</div>

in the debug, it is triggered one time, but I don’t understand in this case I see the tag twice in the dev tool.

---

<div class="post-metadata">

**Author:** ![Oliwer\_Kaczmarek](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/oliwer_kaczmarek/32/3888_2.png) [@Oliwer\_Kaczmarek](https://community.piwik.pro/u/Oliwer_Kaczmarek)\
**Post date:** [March 28, 2023, 11:37am UTC](https://community.piwik.pro/t/user-id-tracking-implementation/1886/9 "2023-03-28T11:37:06Z")

</div>

If the GA have some API for checking what is the user id on the frontend than you could check if it’s empty before sending new one. in Piwik PRO we have:

> \_paq.push([function () {  
> console.log(this.getUserId());  
> }]);

Maybe Google have something similar that you could use to check if the value was already set.
