Google Analytics - using Customer ID as USER_ID

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
8 年 前
Quite a useful feature of Google Analytics is being able to track user journeys of people logged into websites using a unique tracking number, called the USER_ID. This feature is documented here.

I am using the default Google Analytics widget in my nopCommerce installation. My question is around how best to pass the ID of a customer to Google Analytics as the USER_ID when they log on. The CustomerController's Login ActionResult fetches the customer at the moment they successfully log in, but I'm not sure how best to pass that customer's ID to the Analytics script, particularly since logging in can direct users to a variety of landing pages depending on their journey prior to logging in, so I can't simply add a parameter to the

return Redirect(returnUrl);


without changing every potential returnUrl path to expect a parameter.

Any help is appreciated, thanks in advance.
8 年 前
Wouldn't it be easier to just modify the Analytics plugin controller where it injects the script to check the _workContext.CurrentCustomer object and add it at that point?  I've never implemented the user tracking you linked to, but it probably needs to be passed to Google on every page, rather than just once after the customer logins.
8 年 前
Thanks a lot for suggestion. I've just created a work item for this task
8 年 前
ron.richardson wrote:
Wouldn't it be easier to just modify the Analytics plugin controller where it injects the script to check the _workContext.CurrentCustomer object and add it at that point?  I've never implemented the user tracking you linked to, but it probably needs to be passed to Google on every page, rather than just once after the customer logins.


Thanks for the suggestion Ron, this makes sense. I will implement it this way for now.

a.m. wrote:
Thanks a lot for suggestion. I've just created a work item for this task


Thanks Andrei, great to hear!
8 年 前
Commenting with a solve in case anyone else needs a help on this issue:

In Nop.Plugin.Widgets.GoogleAnalytics/Controllers/WidgetsGoogleAnalyticsController.cs

between
analyticsTrackingScript = analyticsTrackingScript.Replace("{ECOMMERCE}", "");

and
return analyticsTrackingScript;


add:
// Push the customer ID into the Analytics script as UserId
            if (_workContext.CurrentCustomer.CustomerRoles.Any(cr => string.Equals(cr.Name, "Registered")))
            {
                analyticsTrackingScript = analyticsTrackingScript.Replace("{USERID}", _workContext.CurrentCustomer.Id.ToString());
            }
            else
            {
                analyticsTrackingScript = analyticsTrackingScript.Replace("_gaq.push(['_setCustomVar',1,'UserID','{USERID}',1]);", "//_gaq.push(['_setCustomVar',1,'UserID','{USERID}',1]);");
            }


and in the Google Analytics widget configuration menu at /Admin/Widget/ConfigureWidget?systemName=Widgets.GoogleAnalytics

between
_gaq.push(['_setAccount', '{GOOGLEID}']);

and
_gaq.push(['_trackPageview']);


add:
_gaq.push(['_setCustomVar',1,'UserID','{USERID}',1]);
6 年 前
And finally done.
Now you can configure Google Analytics plugin to include customer identifier to script. Changes are here.
Thanks for the suggestion.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.