Google analytics, event settings on checkout

  • Posts: 127
  • Thank you received: 3
3 years 10 months ago #319805

Good Morning

Perfect, but this does not affect the tracking of the "next" buttons, the code would be copied below.

A question that arises for me is: are the gtag statements executed when the buttons are clicked or when the buttons are loaded on the page? because I detect follow-up of the button to finish the purchase (step == 4), as it was clicked 4 times on it, but there are no purchases in the hikashop panel

window.Oby.registerAjax (['checkoutFormSubmit', 'checkoutBlockSubmit'], function (params) {
var mainDiv = document.getElementById ('hikashop_checkout');
var step = mainDiv.getAttribute ('data-checkout-step');
if (step == 1) {
gtag ('event', 'next_buy', {'event_category': 'payment_process', 'event_label': 'next_buy'});
}
if (step == 2) {
gtag ('event', 'next_session_start', {'event_category': 'paid_process', 'event_label': 'next_session_start'});
}
if (step == 3) {
gtag ('event', 'next_address', {'event_category': 'payment_process', 'event_label': 'next_address'});
}
if (step == 4) {
gtag ('event', 'final_payment_method', {'event_category': 'paid_process', 'event_label': 'finalize_payment_method'});
}
});
Thank you very much, greetings!

Last edit: 3 years 10 months ago by nicolas.

Please Log in or Create an account to join the conversation.

  • Posts: 81515
  • Thank you received: 13069
  • MODERATOR
3 years 10 months ago #319810

Hi,

With this code, the gtag call is only done when the button is pressed, not when the page is loading.
Now, if an error happens (for example, the customer didn't check the Terms and Conditions checkbox), then the server will refuse the creation of the order and even though gtag will be triggered, the order won't be created. So it's totally normal.
You should add an additional gtag call in the checkout / after_end.php view file, which displays the "thank you" page when the customer comes back from the payment gateway and potentially another one in checkout / end.php which displays the redirection page when the customer goes to the payment gateway.

Please Log in or Create an account to join the conversation.

  • Posts: 127
  • Thank you received: 3
3 years 10 months ago #319867

Hello

Of course I understand, it worked perfectly.

Now, what is not clear to me is about adding the JS code in the PHP view, in the end.php view would I have to add a hidden html tag that in the onload event of the page calls a JS function that executes the gtag sentence?

Thanks greetings !

Please Log in or Create an account to join the conversation.

  • Posts: 81515
  • Thank you received: 13069
  • MODERATOR
3 years 10 months ago #319870

Hi,

In the code of the view, at the beginning, before anything else, you can directly add:

<script>... your JS ... </script>
and the JS wil run on that page when it loads.
You can wrap your JS in an onload event so that it runs only after the script files are loaded (and mainly the one creating the gtag function :)

Please Log in or Create an account to join the conversation.

  • Posts: 127
  • Thank you received: 3
3 years 10 months ago #319918

Good Morning

Great, it's very clear to me.

Thank you very much for your great help

Please Log in or Create an account to join the conversation.

Time to create page: 0.064 seconds
Powered by Kunena Forum