nystudio107 / craft-instantanalytics-ga4

Instant Analytics brings full Google GA4 server-side analytics support to your Twig templates and automatic Craft Commerce integration
Other
3 stars 4 forks source link

Track user ID #10

Closed Romanavr closed 1 year ago

Romanavr commented 1 year ago

Is your feature request related to a problem? Please describe.

My feature request is based on this issuehttps://github.com/nystudio107/craft-instantanalytics-ga4/issues/6. After some research in the GA4 docs and in your plugin, I found that we're not sending the userId in any way, so I think that's the main problem. Even if not, it would still be good to send the userId.

Describe the solution you would like

I guess we can add it in src/ga4/Analytics.php and as a user id value: if the user is logged in, the user id, if not and it's an anonymous order, use the short order number (to avoid conflict with user and order id), for example:

$userId = Craft::$app->user->getId();
if (!$userId) {
    $userId = (new \craft\commerce\services\Carts())->getCart()->shortNumber;
}
$request->setClientId($userId);

Describe alternatives you have considered

I don't see any alternatives here. Maybe you can suggest, @khalwat ?

Additional context

https://support.google.com/analytics/answer/9213390 https://support.google.com/analytics/answer/9355972?hl=en

"For example, a user starts a session with no associated user ID and triggers Events 1 and 2. No user ID is associated with those events. The user then signs in and triggers Event 3. Events 1, 2, and 3 are now all associated with that user's ID. The user finally signs out and then triggers Event 4. No user ID is associated with Event 4. Events 1, 2, and 3 remain associated with that user."

khalwat commented 1 year ago

We think we may have tracked down what's going on here.

Check out version 4.0.0-beta.3 -> https://github.com/nystudio107/craft-instantanalytics-ga4/releases/tag/4.0.0-beta.3

...and the docs changes/additions -> https://nystudio107.com/docs/instant-analytics-ga4/using.html#craft-commerce-tracking-with-google-enhanced-ecommerce