setUserId()

The setUserId() method sets the user ID for a visitor. It can be the same user ID as in your CMS, CRM or sales system. The user ID helps to identify a visitor across devices.

Syntax

setUserId(userID)
setUserId(userId): void
setUserId(userId): void
setUserId(userId): void
setUserId(userId): void
setUserId(userId): void
setUserId(userId): void

Parameters

userID (string, required)

A non-empty, unique ID of the visitor. Example: ABC123. It needs to be unique for each visitor. Can be up to 1024 bytes (1024 ASCII characters).

Examples

To set the user ID as ABC123 and send it to Piwik PRO:

_paq.push(["setUserId", "ABC123"]);  
_paq.push(["trackPageView"]);
setUserId("ABC123"): void
setUserId("ABC123"): void
setUserId("ABC123"): void
setUserId("ABC123"): void
setUserId("ABC123"): void
setUserId("ABC123"): void

Notes

  • Calling the setUserId() method doesn't send any data to Piwik PRO. To send the user ID to Piwik PRO, call the trackPageView() method after calling the setUserId() method, or send a ping request.
  • It's a good practice to remove the user ID value after the user logs out. This helps to correctly track sessions for other users who may be using the same device. To remove the user ID call the resetUserId() method.
  • Never use an empty value when setting the user ID – it'll mess with the tracking. To reset the value call the resetUserId() method.
  • The user ID helps you recognize a visitor across many devices. For example, a visitor is recognized as the same visitor when they switch between a laptop, smartphone and tablet.
  • The user ID helps to assign sessions to the visitor and is sometimes used as a last resort identification mechanism, for example, when the visitor has disabled cookies.
  • If the user ID is set for a visitor, its value overwrites the visitor ID, but it doesn’t serve as a mechanism for joining sessions in cross-device tracking. Sessions from different devices are split to get a clearer picture of user behavior.
  • [The](doc:The user ID can also be set with Tag Manager. [Read more]) user ID can also be set with Tag Manager. Read more

Related methods