aawnu / php-ga4

PHP Wrapper for Google Analytics 4 with Server Side Tracking

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

php-ga4: ^1.1 - Events are not appearing in GA4 realtime .

dhufish opened this issue · comments

Hi there,
I'm trying to use your package to record GA4 backend events and I'm not having much luck. I'm using Laravel 8 with php 8.1.
The code seems to execute (exception code does not fire), however the events are not appearing, specifically add_to_cart. Here is my code...

`
$measurementId = env('GA4_MEASUREMENT_ID');
$ga4ApiSecret = env('GA4_API_KEY');

    $clientID = null;

    if(isset($_COOKIE['_ga'])) {
        $gaCookie = $_COOKIE['_ga'];
        $clientID = explode('.', $gaCookie)[2];
    }

    try {
        $events = Converter::parseEvents($event);
        Analytics::new($measurementId, $ga4ApiSecret)
            ->setClientId($clientID)
            ->addEvent(...$events)
            ->post();
    } catch (Exception\Ga4Exception $exception) {
        // Handle exception
        Log::alert('GA4 Event failed');
        Log::alert($exception);
        Log::alert($event);

    }

`

My event looks like this...

array ( 'add_to_cart' => array ( 'currency' => 'AUD', 'value' => 38, 'items' => array ( 0 => array ( 'item_id' => 29, 'item_name' => '500g Musk Scrolls', 'price' => 38, 'quantity' => 1, ), ), ), )

The GA4 credentials and event data appear to be fine as I have recorded the event with another package but I like the way this package calls are made and it better fits my application.

Can you suggest how I can debug the problem? Many thanks

I found a problem with my event, the array index 'add_to_cart' is incorrect, I changed it to 'AddToCart' to match the correct class name...

I am now facing this...
AlexWestergaard\PhpGa4\Event\AddToCart::addItem(): Argument #1 ($item) must be of type AlexWestergaard\PhpGa4\Facade\Type\Item, array given

I suspect my $event array is not correctly formed and is causing the problem here... from my axios post I am creating it with...
$event = $request->all();

What is the best way to pass this in to Converter::parseEvents()?

Hello,

Would you mind passing the request body you push to the converter? Then I will have a look and see what I can do 🥳

Edit: just saw you passed the array, I'll look at it

@dhufish I have located the flaw in my conversion. It did not render item array into item model before applying it to the event model. This will be fixed in the next version.

PLEASE read the release statements as there have been major changes in the past few releases to improve the readability of this library.

NOTE I will let you know when I release 1.1.3-beta so that you can use this update ASAP.

Re-opens to notify on beta release.

Thank you for the update I appreciate you taking the time to respond quickly. I will keep an eye out 1.1.3-beta and provide feedback when available. 😎

Hello @dhufish

I have released v1.1.3-beta, make sure to test it out locally first to make sure you are up to date with latest changes. You can run this pre-release by 1.1.3-beta as composer version.

Hi @aawnu, Thank you again for looking in to this issue, I updated to 1.1.3-beta and ran some add_to_cart tests and they appeared in the GA4 realtime dashboard 👍 I'm adding more events over the next week so I'll keep you updated if any issues. Much appreciated and I look forward to contributing to your project.

Hi @aawnu, I tried two more events and both silently failed to appear in GA4 dashboard... BeginCheckout and Purchase... no errors were thrown, here are the events that were passed...

0 => 
  AlexWestergaard\PhpGa4\Event\BeginCheckout::__set_state(array(
     'isCloning' => false,
     'currentIteratorKey' => 0,
     'allIteratorKeys' => 
    array (
      0 => 'language',
      1 => 'page_location',
      2 => 'page_referrer',
      3 => 'page_title',
      4 => 'screen_resolution',
      5 => 'currency',
      6 => 'value',
      7 => 'coupon',
      8 => 'items',
    ),
     'campaign' => 
    array (
    ),
     'currency' => 'AUD',
     'value' => 38.0,
     'items' => 
    array (
      0 => 
      array (
        'item_id' => '13',
        'item_name' => '500g  Hey Girl',
        'item_category' => 
        array (
          0 => 'Pre Made Candy',
        ),
        'price' => 38.0,
        'quantity' => 1,
      ),
    ),
  ))
0 => 
  AlexWestergaard\PhpGa4\Event\Purchase::__set_state(array(
     'isCloning' => false,
     'currentIteratorKey' => 0,
     'allIteratorKeys' => 
    array (
      0 => 'language',
      1 => 'page_location',
      2 => 'page_referrer',
      3 => 'page_title',
      4 => 'screen_resolution',
      5 => 'currency',
      6 => 'transaction_id',
      7 => 'value',
      8 => 'affiliation',
      9 => 'coupon',
      10 => 'shipping',
      11 => 'tax',
      12 => 'items',
    ),
     'campaign' => 
    array (
    ),
     'currency' => 'AUD',
     'transaction_id' => '8',
     'value' => 38.0,
     'items' => 
    array (
      0 => 
      array (
        'item_id' => '13',
        'item_name' => '500g  Hey Girl',
        'item_category' => 
        array (
          0 => 'Pre Made Candy',
        ),
        'price' => 38.0,
        'quantity' => 1,
      ),
    ),
  ))

@dhufish please make new issues when a ticket is closed, because it does not come up. Only saw it because I have emails activates on all.