How to programmatically measure a purchase in Google Analytics through Google Tag Manager after a successful POST api response?

Looking through the documents for Google Tag Manager, it seems that they offer to measure the purchase by viewing confirmation pages. If I misinterpret this, it may be inaccurate due to a failure during redirection or artificial overestimation, as the link can be visited later.

So, I think it would be preferable to register or measure the purchase as soon as I receive a successful response from my POST request for the purchase. Something like that:

function purchase(data, form) {
  fetchHelper(url, {
    method: 'post',
    body: JSON.stringify(data),
  })
    .then(json => {
      // Send info to GTM that purchase was successful
      form.reset()
      window.location.href = `confirmation.html?booking=${json.booking_number}`;
    })
    .catch(error => {
      console.error('Error: ', error)
      swal('Oops', error.message, 'error');
    })
}
+4
source share
2 answers

, /, GA Google analytics.js. GA "", /.

https://developers.google.com/analytics/devguides/collection/analyticsjs/events

GTM Universal Analytics. Track Type Event, .

. , , "purchase_complete". :

function fire(){
  var dataObject = {
    'event': 'purchase_complete',
    'category': 'click',
    'label': 'label_something'
  };
  if(typeof dataLayer != 'undefined'){
    dataLayer.push(dataObject);
  }
}

( https://jonathanmh.com/custom-javascript-trigger-functions-google-tag-manager/)

+1

" "
, . 100% - , . , , . dataLayer (. ). cookie , cookie

" "
, , , "" ( " " ) GA, GA. 2 : . , , , , , " ", , - , .

0

Source: https://habr.com/ru/post/1695547/


All Articles