elgentos / magento2-serversideanalytics

Server side analytics for Magento 2
MIT License
64 stars 21 forks source link

Client ID not correct #20

Closed JoostWan closed 8 months ago

JoostWan commented 1 year ago

The Client ID is retrieved from the Cookie _ga.

The prefix is GA1 but the part of the code does the checking for GA4 and UA1. So there is no match and the code returns null. Now the module created a random Client id and the data is Google Analytics is wrong because the conversion uses a different client id then the original from the visitor session.

@indykoning maybe you can check this out, I saw the addition came from you

if (
            $gaCookieVersion != 'GA' . $this->gaclient->getVersion() &&
            $gaCookieVersion != 'UA' . $this->uaclient->getVersion()
        ) {
            $this->logger->info('Google Analytics cookie version differs from Measurement Protocol API version; please upgrade.');
            return null;
        }

https://github.com/elgentos/magento2-serversideanalytics/blob/master/Observer/SaveGaUserId.php#L132

Some info;

Every Client ID has the structure: GA.1.1 + unique identifier + Unix timestamp. The Unix timestamp is the exact time and date the user first interacted with your pag

https://louder.com.au/2022/06/27/client-id-in-ga4-what-is-it-and-how-to-get-it-in-your-report/

We created a simple patch for testing with some clients and it's working.

@package elgentos/serversideanalytics2

diff --git a/Observer/SaveGaUserId.php b/Observer/SaveGaUserId.php
--- a/Observer/SaveGaUserId.php 
+++ b/Observer/SaveGaUserId.php (date 1678796766430)
@@ -129,10 +129,7 @@
             return null;
         }

-        if (
-            $gaCookieVersion != 'GA' . $this->gaclient->getVersion() &&
-            $gaCookieVersion != 'UA' . $this->uaclient->getVersion()
-        ) {
+        if ($gaCookieVersion != 'GA' . $this->uaclient->getVersion()) {
             $this->logger->info('Google Analytics cookie version differs from Measurement Protocol API version; please upgrade.');
             return null;
         }
mattiasghodsian commented 1 year ago

Am getting Google Analytics cookie version differs from Measurement Protocol API version when an order has been placed. I checked the cookies _ga and this is what i see

image

barryvdh commented 1 year ago

My cookies are also in this format: GA1.2.123456789.1234567890 That means $gaCookieVersion = GA1. But the GA Clientversion is hardcoded to 4, so that will indeed never match.

That seems to be described here also: https://www.optimizesmart.com/google-analytics-cookies-ultimate-guide/

image
JoostWan commented 1 year ago

@barryvdh We created more changes for this module and testing it on some clients. You needed also the session_id otherwise the transactions can't related to a previous session in GA4.

barryvdh commented 1 year ago

PR in https://github.com/elgentos/magento2-serversideanalytics/pull/21 Can you try?

barryvdh commented 1 year ago

@JoostWan Oh sorry, only see your message now. Can you create a (draft) pull request or send the diff/patch? Then I'll test as well.

JoostWan commented 1 year ago

@barryvdh I will create a pull request later this week. Need to remove the UA from the code also because it's not needed anymore.

WouterSteen commented 1 year ago

Yes, we Will try it also then! Nice work! Very much appreciated!!

JoostWan commented 1 year ago

@barryvdh @WouterSteen Can you check if it is working properly on your end as well?

https://github.com/elgentos/magento2-serversideanalytics/pull/22

peterjaap commented 8 months ago

I guess we can close this issue now, judging from #22.