Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GA4: purchase event wrong #3228

Closed
pquerner opened this issue May 3, 2023 · 11 comments
Closed

GA4: purchase event wrong #3228

pquerner opened this issue May 3, 2023 · 11 comments

Comments

@pquerner
Copy link
Contributor

pquerner commented May 3, 2023

PR #3092 introduced the usage of the purchase event trigger for GA4, however I believe its not fully functional.

Preconditions (*)

  1. 20.1.0-rc3 or 19.5.0-rc3 required (implementation of GA4)
  2. GA4 must be configured in the backend and ready to use

Steps to reproduce (*)

  1. Configure GA4 and make a purchase
  2. The purchase event should not be triggered
  3. Reason is, that according to https://developers.google.com/analytics/devguides/collection/ga4/reference/events?client_type=gtag#purchase the shipping, tax and value and for the item, the price and quantity should be of number format.
  4. It is however a string value (ie: according to number_format)

Expected result (*)

  1. The event purchase should be triggered

Actual result (*)

  1. The event is not triggered (due to inaccurate purchase event structure)

Fix would be to cast the values of value, shipping and tax via floatval to a float value.

'value' => number_format($order->getBaseGrandTotal(), 2),

and here

'quantity' => $item->getQtyOrdered(),
'price' => $item->getBasePrice(),
'discount' => $item->getBaseDiscountAmount()

(int cast for quantity and floatval for price and discount)

@fballiano
Copy link
Contributor

getBasePrice and getBaseDiscountAmount are already floats, while quantity could also be decimal so we can't cast it to int

@pquerner
Copy link
Contributor Author

pquerner commented May 3, 2023

In my testing the items price is a string value. Might be some wierd thing on our end then..
In our case qty also can't be float, hence my wrong assumptions then. Sorry^^

@fballiano
Copy link
Contributor

can you try #3231?

although I have the same kind of code on a live store and it works fine

@fballiano
Copy link
Contributor

mmmm it is true that $item->getBasePrice() does return a string instead of a float (although it's documented otherwise)...

@pquerner
Copy link
Contributor Author

pquerner commented May 3, 2023

You said you have this running on a client, can you confirm (if you have access to the GA account) that the conversions are empty? Or maybe Google is graceful (enough) that it does the typecasting for us?

Sadly I cannot test the PR on our site, since I am in the process of implementing it myself and all my ways of adding a "event purchase" so far have failed (atleast are not visible). Not sure whats going on, hence my first idea the typejuggling thingy here.
(Or maybe google aggregates my data 24 hours later..)

@fballiano
Copy link
Contributor

conversions are NOT empy for my customers, although I'm using an older implementation of the GA4 PR, since it was the one that I then ported on the core.

the data are aggregated at least 24 hours later, if it is a new implementation.

@pquerner
Copy link
Contributor Author

pquerner commented May 3, 2023

Ok, thanks for clarifying. Which then means that some error is on my side and the original PR works just fine (?).

I have something like this running locally now, will report back when Google shows me the data. fingers crossed

@fballiano
Copy link
Contributor

what is the error you're getting?

@fballiano
Copy link
Contributor

it would seem very weird anyway that a type cast invalidates the tracking in analytics, they will for sure try to cast on their end not to lose any transaction data, I think.

@pquerner
Copy link
Contributor Author

pquerner commented May 3, 2023

The 'error' is I am getting no purchase data.

But then again, it may be because of our implementation.
I am debugging still, but I saw the event structure documentation and saw the difference in types, hence this issue report.

// Edit
Like you said, you have data for your customers, so this issue cant be the cause of it. Although it would not hurt to have correct data types, right? ^^

@pquerner
Copy link
Contributor Author

pquerner commented May 8, 2023

Update:
I've fixed our GA4 code and can confirm nothing is wrong with the current iteration of the GA4 implementation.

Seems like google is gracefully parsing the values anyways.
I have confirmed this by turning on debug mode and made a purchase once with the current implementation and once with "proper typed values". In each case the values got send correctly.

So I think its best to close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants