Tracking of user ids in Google Analytics scripts gives you the following benefits (see Benefits of using the User ID feature):
- Get a more accurate user count
- Analyze the signed-in user experience
- Access special tools and reports in your Analytics account
- Find relationships between your acquisitions, engagement, and conversions
In order to enable it in Sharepoint we need to provide user id of the currently logged in user. On the client side we may get user id of the current user using _spPageContextInfo.userId propery. However _spPageContextInfo object may not be available yet when GA script is executed (see How to get URL of current site collection and other server side properties on client site in Sharepoint about details of internal implementation of this object). In this case we may use delayed script execution in Sharepoint. If regular GA script looks like this;
Then script with enabled tracking of user ids for Sharepoint will look like this:
Note that we used ExecuteOrDelayUntilScriptLoaded function in order to ensure that _spPageContextInfo is loaded.