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

If you follow the code then you will see there is 3 layers. #57

Closed
srakl opened this issue Jun 25, 2023 · 1 comment
Closed

If you follow the code then you will see there is 3 layers. #57

srakl opened this issue Jun 25, 2023 · 1 comment
Labels
question Further information is requested wontfix This will not be worked on

Comments

@srakl
Copy link

srakl commented Jun 25, 2023

If you follow the code then you will see there is 3 layers.

Analytics[ Events[ Items ] ] ]

Add src/Item.php to your src/Event/BeginCheckout.php event, then add the event to src/Analytics.php.

You should not be able to addItem on Analytics, if this is doable then I will fix it later.

Originally posted by @aawnu in #55 (comment)

sorry i really dont understand what you mean. how do i add items to my events?

in your begincheckout class you do have this

  public function addItem(Facade\Type\ItemType $item)
    {
        $this->items[] = $item->toArray();
        return $this;
    }

and i did add it.

$analytics->addItem([
                'item_id' => 'bla bla',
                'currency'=> 'USD',
                'price'   =>'60.00',
                'quantity'=> 1
            ]);
@aawnu
Copy link
Owner

aawnu commented Jun 25, 2023

Okay, you need to first populate an item, then you need to make your event and pass the item to that event, and finally add it to the Analytics call.

$item = AlexWestergaard\PhpGa4\Item::new() // This is a CLASS not an ARRAY
    ->setItemId('blabla')
    ->setCurrency('usd')
    ->setPrice(60)
    ->setQuantity(1);

$event = AlexWestergaard\PhpGa4\Event\BeginCheckout::new()
    ->{your params here}
    ->addItem($item); // <--- You need to add the item to the EVENT, not ANALYTICS

$analytics = AlexWestergaard\PhpGa4\Analytics::new('id', 'secret')
    ->setClient('bla')
    ->addEvent($event); // You need to add the EVENT to ANAYLTICS, where in EVENT contains ITEMS

$analytics->post(); // This you want a try-catch around as is throws on errors, it returns void

You can NOT add Items to ANALYTICS and you can not pass ARRAY as argument as it is validated on items->toArray() that all REQUIRED parameters are set inside of src/Helper/IOHelper.php.

--- EDIT

The reason it is separated is because you might want to sent multiple events with the same items, this way you can easliy add the items to multiple events by reference.

--- EDIT 2

This also enabled that the same events can be sent to multiple Analytics instances, for example some e-comm websites have a global tracker for all shops and then a separate tracker per shop.

@aawnu aawnu added question Further information is requested wontfix This will not be worked on labels Jun 25, 2023
Repository owner locked as resolved and limited conversation to collaborators Jun 25, 2023
@aawnu aawnu closed this as completed Jun 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question Further information is requested wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants