br33f / php-GA4-Measurement-Protocol

PHP GoogleAnalytics4 Measurement Protocol Library
88 stars 19 forks source link

GA4 doesn't match conversion with existing data #24

Closed JoostWan closed 1 year ago

JoostWan commented 1 year ago

I don't know if this is an issue or a wrong call on our side. But we used this package from e-commerce system Magento. When a payment is made we send a call to the measurement protocol. We saved also the client_id to the order. When we do the call to the measurement protocol we send also the client ID. Conversion is visible in GA4 but it's not matched with an existing sessie. Did some research and found some information on this site;

How we can set the Session ID and timestamp with this package to send this data with the client id to the measurement protocol.

https://www.simoahava.com/analytics/session-attribution-with-ga4-measurement-protocol/

Screenshot 2023-03-14 at 21 56 29

br33f commented 1 year ago

@JoostWan You can set any data to an event object:

$eventName = 'example_event'; 
$anyEventData = new BaseEvent($eventName);
$anyEventData
    ->setMethod('Twitter')
    ->setContentType('Post')
    ->setItemId('example_item_id')
    ->setAnyParamYouWish('test'); // means 'any_param_you_wish' is set

So you can set both session_id and timestamp parameter this way.

Alternatively, if you wish not to use PHP magic methods, you can set params explicitly by calling: $anyEventData->setParamValue('param_name', 'param_value');

Hope it helps.

JoostWan commented 1 year ago

@br33f Thanks!

Luxato commented 1 year ago

@JoostWan Hey, I am currently solving exactly the same problem, all my conversions are being attributed as "direct" even though most of them should come from "Paid" and "Organic".

Can I ask if I have to send the whole value of the cookie with the 2 letters in the beginning? For example for _ga, should it be GA1.1.1938356095.1686127670 or just 1.1.1938356095.1686127670 Same for ga, GS1.1.1686131519.2.1.1686131605.60.0.0 or 1.1.1686131519.2.1.1686131605.60.0.0.

Thanks for reply.

basepack commented 1 year ago

@Luxato you could check our conversation here to see how you get those properties: https://github.com/br33f/php-GA4-Measurement-Protocol/issues/33

Luxato commented 1 year ago

@basepack Thank you. I actually found this video where all required parameters are explained in detail. https://www.youtube.com/watch?v=r_eoeU2qUn0

Luxato commented 1 year ago

But I still can't attribute them with the correct sessions, every conversion is still "direct" ffs. Have you eventually made it run @basepack ?

Fnizou commented 1 year ago

@Luxato You have use : $eventData->setParamValue('session_id', 'session-id-here'); Or $eventData->setSessionId('session-id-here'); ?

Luxato commented 1 year ago

@Fnizou Yes, I am using this

if (strpos(($browserData['ga4_session_id'] ?? ''), '.') !== false) {
                $parts = explode(".", $browserData['ga4_session_id']);
                $ga4_session_id = $parts[2]; // Output: 1686042977
}
$purchaseEventData->setParamValue('session_id', $ga4_session_id);
Fnizou commented 1 year ago

@Luxato I will try on my side in the next few days,

can you try to force the typing: $purchaseEventData->setParamValue('session_id', (int)$ga4_session_id);

I have already had a problem with the transaction_id being raised because of this,

we stay informed

Fnizou commented 1 year ago

@Luxato on my side I just set up this, and I think "session_id" it's a (string) and not (int),

if( !is_null($row['id_session_ga4']) ){ $purchaseEventData->setParamValue('session_id', (string)$row['id_session_ga4']); }

I wait 24 hours

Luxato commented 1 year ago

I've been investigating my data in BigQuery. It seems like it gets there fine. But when I tried to search by client id, there are no prior events. Then I checked other events and I've noticed that the client id is encoded.

image

Whereas my conversions have format taken from the cookie image

Any ideas why it's being encoded?

Luxato commented 1 year ago

image

Found this setting in the GTM server container :P, the client id is not being encoded anymore

Kurtafterjim commented 11 months ago

Hi, I have the same problem: in the "User acquisition" report, purchases are correctly attributed to the various channels, while in the "Traffic acquisition" report, all conversions are attributed to the "Unassigned" channel group. I realized the reason is that I set setClientId() but not setSessionId().

However, I didn't understand how to extract the Session ID value. Can you help me please? Thank you