26

The Google Analytics User ID documentation page shows that you set the User ID when you create the tracker. I have a javascript only project with sign-in after page load, is there any way to set the User ID after login? Something like ga("set", "userid", "UNIQUEID")

On the field reference page it says you can only set user-id in the create function. Are we able to call create again with the user-id? Will that create a new tracker, or override the old one?

EDIT: The beta version of User ID tracking showed that you could specify the userid after creating the tracker using ga('set', '&uid', {{ USER_ID }}); and that would try to unify the session from before the userid was set with the one after. That seems to be very different than what the docs current specify. Is there any way to do this with the current method?

5 Answers 5

23

This one works

ga('set', '&uid', '<dynamic user id here>');
Sign up to request clarification or add additional context in comments.

8 Comments

So ga('set', '&uid', {{ '<dynamic user id here>' }}); would be wrong? It isn't really clear from the docs, and this is the first thing I have seen that doesn't include the double curly.
Dude, the Google docs themselves have the double curly brackets as part of their example, which is why I was asking.
I don't know what to think "these braces mean". They were clearly confusing as I ended up here and so did Sean and Eli. The programming language being used is javascript. Templating libraries often use double curly braces, but it should be clear that Google Analytics uses no such library and so no "crazy technologies" are being used here. The GA examples and docs are confusing, it should just say ga('set', '&uid', 'USER_ID'); IMO.
The double curly braces are a variable place holder- they're used in Angular and the Django template language.
@robbyt Kind of stupid of Google to assume that everyone is using a templating system though. I wouldn't make it part of the documentation which is supposed to be universally understood. Most people won't even be using templating.
|
23

The documentation seems to indicate that, with analytics.js, you should do the following:

// Alternatively, you may set the user ID via the `set` method.
ga('set', 'userId', 'as8eknlll');

I'm not sure how this differs from the current answer (which uses &uid), but this solution is working for us.

5 Comments

&uid is the 'measurement protocol' (at HTTP level) parameter (developers.google.com/analytics/devguides/collection/protocol/…) and userId is the field name (at javascript level). Both are valid and should be equivalent I believe.
i m using same way to set the user id but how can i fetch the details in my webpage for specific user id. please help in this post stackoverflow.com/questions/45419397/…
Question: If my user ids will always be numeric, do I still need the single quotes around the ID? Can it be ga('set', 'userId', 1234);
From what I can tell here (developers.google.com/analytics/devguides/collection/protocol/…) and here (developers.google.com/analytics/devguides/collection/…) it looks like they expect a string. Not sure if they'd do that conversion for you or not.
@blimmer, thanks. I see this now: Important! The USER_ID value should be a unique, persistent, and non-personally identifiable string identifier that represents a user or signed-in account across devices.
7

You should set userId beofre sending event to Google Analytics. I couldn't manage to send userId after sending events pageview and ecommerce:send.

Comments

1

It appears from Justin Cutroni's blog that he's saying that data stitching (in your case matching the userid prior to login to the userid post-login) is not currently offered in Google Analytics.

http://cutroni.com/blog/2014/04/10/understanding-cross-device-measurement-and-the-user-id/

See specifically this sentence: "Google Analytics will not go back in time and stitch every single session from a given user together."

Comments

0

If you are using Google Tag Manager you can set the User ID by added '&uid' as a field to your Google Analytics Setting variable:

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.