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

Automatic Commerce Events Never Trigger #21

Closed phillmorgan28 closed 11 months ago

phillmorgan28 commented 11 months ago

Describe the bug

The automatic Commerce events never trigger

To reproduce

Steps to reproduce the behaviour:

  1. Enable automatic firing for all events in the plugin settings
  2. Ensure that the Craft log level is INFO or use dev mode
  3. Add item to the basket
  4. Examine log - note that the plugin does not log the event firing
  5. Examine log - note that the plugin does log page views indicating correct installation
  6. Examine GA4 purchase journey - note that the add to basket event has not been received

Expected behaviour

The add to basket event (and others) to fire and be received by GA4

Log file test

2023-10-25 11:13:35 [web.INFO] [nystudio107\instantanalyticsGa4\InstantAnalytics::init] Instant Analytics GA4 plugin loaded {"memory":32484864} 
...
2023-10-25 11:13:35 [web.INFO] [nystudio107\instantanalyticsGa4\services\Ga4::addPageViewEvent] pageView event queued for sending {"memory":68110496} 
2023-10-25 11:13:35 [web.INFO] [nystudio107\instantanalyticsGa4\ga4\Analytics::sendCollectedEvents] Sending 1 analytics events {"memory":68121080} 

Versions

khalwat commented 11 months ago

How exactly are you adding an item to the basket? What Commerce APIs are you using to do so?

phillmorgan28 commented 11 months ago

I'm using the standard Commerce HTML form to add items to the basket. Excerpt below.

          <form method="POST">
            <input type="hidden" name="successMessage" value="{{ 'Item has been added to your basket.' | t | hash }}">
            <input type="hidden" name="purchasableId" value="{{ product.defaultVariant.id }}">
            {{ actionInput('commerce/cart/update-cart') }}
            {{ redirectInput(addToBasketRedirect('basket')) }}
            {{ csrfInput() }}

I've confirmed this is calling the Commerce line item events for before and after one is added, using my own plugin event hooks.

khalwat commented 11 months ago

That's really strange... is it calling through to the event listeners that Instant Analytics adds? Will try to reproduce locally, but we have confirmed it does listen for these events.

phillmorgan28 commented 11 months ago

Thanks for looking into this one. I've had a look at the plugin code myself and I can't see any issues that would prevent the hooks from working. I'll try to get approval from our client to start debugging our end to see if something else is getting in the way. I'll report back any findings if applicable to the plugin. Thanks again

phillmorgan28 commented 11 months ago

Hi, thanks for taking the time to check on this for me. I've been able to confirm that the plugin IS sending the events after enabling info level logging in Craft. Our client was using a closed funnel (Purchase Journey) in GA4 which was missing a step, so made it look as though events were not coming through. I've added the product/item view GA4 event using the manual twig tag which should solve the issue.