nystudio107 / craft-instantanalytics-ga4

Instant Analytics brings full Google GA4 server-side analytics support to your Twig templates and automatic Craft Commerce integration
Other
3 stars 4 forks source link

view_item formatting. #27

Closed pixelmachine closed 7 months ago

pixelmachine commented 8 months ago

Question

The documentation has an example of manually creating and sending a view_item event. However the example doesn't show how to add the nested items that are needed. Looking at the code and GA4 MP docs this is what I've come up with:


{% set viewItem = instantAnalytics.create.ItemParameter %}
{% do  viewItem.
        .setItemId(product.sku)
        .setItemName(product.title)
        .setCurrency(cart.getPaymentCurrency())
        .setPrice(product.price)
        .setItemCategory(product.type)
        .setQuantity(1)
%}

{% set viewItemEvent = instantAnalytics.create.ViewItemEvent %}
{% do  viewItemEvent
        .setValue(product.price)
        .setCurrency(cart.getPaymentCurrency())
        .addItem(viewItem)
%}

{% do  instantAnalytics.viewItemEvent(viewItemEvent) %}

Does this look correct?

Additionally, what's the best way to debug? I can dump() the objects created but I wondered if there was a better way to see what's happening.

Finally there's a small typo in the docs. After the Event Reference table you have {% do instantAnalytics.viewItemEvent(searchEvent) %} but you should be passing in viewItemEvent not searchEvent.

khalwat commented 7 months ago

Yep, looks right to me. The way you're debugging things is likely fine; you can also use XDebug on the PHP side of things to make it easier.

And then open up your Google Analytics backend and see what is sent there.

I fixed the typo in the docs, thanks.

pixelmachine commented 7 months ago

Thanks @khalwat

It seems Google doesn't show commerce data submissions on new sites until a certain amount has been submitted. So in this case and another previous case it wasn't possible for me to see what data was sent until the site was live and the data was flowing.

khalwat commented 7 months ago

Makes sense -- I wish I knew of a better way. Perhaps they have some kind of sandbox mode that works more immediately for testing?

pixelmachine commented 7 months ago

Not sure, if I find anything I'll let you know!

pixelmachine commented 7 months ago

@khalwat re. the typo I mentioned. I just realised it's still wrong:

{% do instantAnalytics.viewItemEvent(viewItemEvent) %}

Should be

{% do instantAnalytics.addEvent(viewItemEvent) %}

Right?

khalwat commented 7 months ago

haha -- fail. Yep, you're right. Fixed it for real this time.