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

Manually sending a purchase event / ajax blocking #9

Closed bossanova808 closed 1 year ago

bossanova808 commented 1 year ago

Ok, so vis a vis discussion about the ajax blocking the sending of my add to cart, remove from cart, and purchase events, meaning I am not seeing any purchases come through...I am now trying to do this manually but without re-implementing the entire wheel.

In my order complete template I have:

    {% if craft.app.plugins.isPluginEnabled('instant-analytics-ga4') %}
        {% do instantAnalytics.triggerOrderCompleteEvent(order) %}
    {% endif %}

And in the bottom the layout I have

        {% if craft.app.plugins.isPluginEnabled('instant-analytics-ga4') %}
            {% do instantAnalytics.sendCollectedEvents() %}
        {% endif %}

But...this does not work - no event is sent to GA4. I added some logging code to triggerOrderCompleteEvent and that's not coming out either.

Obviously I can add my own code to create and send the event but right now I am already doing that for all of these events manually:

ViewCart
BeginCheckout
Search
ViewItem  (see other issue on this one)

(I am doing those via via my own service in PHP because doing it all in Twig is just harder/messier - those are working ok, although I can't see the lineItem data in GA4 when looking at the begin checkout event, even though I am adding it:

    public function eventBeginCheckout(Order $cart): void
    {
        self::note("GA4 - BeginCheckout Event");
        $event = InstantAnalytics::$plugin->ga4->getAnalytics()->create()->BeginCheckoutEvent();
        $event->setCurrency('AUD');
        $event->setValue($cart->totalPrice);
        if($cart->couponCode) {
            $event->setCoupon($cart->couponCode);
        }
        $this->addCartLineItemData($event, $cart);   
        InstantAnalytics::$plugin->ga4->getAnalytics()->addEvent($event);
    }

(I'd use your function to add the lineItem data but it's protected.)

Thus, since the ajax change, the only things I have actually got going to GA4 from IA with the 'it just works' approach are:

view_item_list
page_view
khalwat commented 1 year ago

@bossanova808 you should be able to get this to work by doing the following in your XHR'd templates:

        {% hook 'iaSendPageView' %}

If this doesn't work for you, I will re-open.

Addressed in 4.0.0-beta.2

bossanova808 commented 1 year ago

Tried a couple quick tests

    {# ANALYTICS - need to trigger this manually as the ajax prevention stuff stops the auto sending... #}
    {% if craft.app.plugins.isPluginEnabled('instant-analytics-ga4') %}
        {% do instantAnalytics.triggerOrderCompleteEvent(order) %}
    {% endif %}

...

{# ANALYTICS - beacuse we do ajax paymnt, force this to be submitted as if AutoSendPageViews is off #}
{% block tail %}
    {{ parent() }}
    {% if craft.app.plugins.isPluginEnabled('instant-analytics-ga4') %}
        {% hook 'iaSendPageView' %}
    {% endif %}
{% endblock %}

..but not seeing a purchase resulting at the GA end still.

(Not seeing any user data either yet despite those client_id changes).

Am actually about to go away for ~10 days, so will have to check back on progress after that. Hopefully other folks will have started to dig in by then.

bossanova808 commented 1 year ago

(I presume that this line:

{% do instantAnalytics.triggerOrderCompleteEvent(order) %}

is not actually needed - the idea being IA is creating a purchase event (when listening to the Commerce event) - so really just that hook is needed to force send it, right?)

khalwat commented 1 year ago

Right only the hook should be needed. I will re-open this so we can double-check it.

khalwat commented 1 year ago

For completeness, here are the events that automatically fire from Commerce events:

        // Commerce-specific hooks
        if (self::$commercePlugin !== null) {
            Event::on(Order::class, Order::EVENT_AFTER_COMPLETE_ORDER, function (Event $e): void {
                $order = $e->sender;
                if (self::$settings->autoSendPurchaseComplete) {
                    $this->commerce->triggerOrderCompleteEvent($order);
                }
            });

            Event::on(Order::class, Order::EVENT_AFTER_ADD_LINE_ITEM, function (LineItemEvent $e): void {
                $lineItem = $e->lineItem;
                if (self::$settings->autoSendAddToCart) {
                    $this->commerce->triggerAddToCartEvent($lineItem);
                }
            });

            // Check to make sure Order::EVENT_AFTER_REMOVE_LINE_ITEM is defined
            if (defined(Order::class . '::EVENT_AFTER_REMOVE_LINE_ITEM')) {
                Event::on(Order::class, Order::EVENT_AFTER_REMOVE_LINE_ITEM, function (LineItemEvent $e): void {
                    $lineItem = $e->lineItem;
                    if (self::$settings->autoSendRemoveFromCart) {
                        $this->commerce->triggerRemoveFromCartEvent($lineItem);
                    }
                });
            }
        }