title: How do I programmatically measure a purchase to Google Analytics through Google Tag Manager after a successful api POST response?
body: <p>Looking through the docs for Google Tag Manager, it seems they suggest measuring a purchase by confirmation page views. Unless I'm misinterpreting this, it could be inaccurate due to a failure during redirect or artificially inflated since the link can be visited later.</p>
  
  <p>So, I think it would be preferable to register, or measure, the purchase as soon as I get a successful response from my purchase POST request. Something like this:</p>
  
  <pre><code>function purchase(data, form) {
    fetchHelper(url, {
      method: 'post',
      body: JSON.stringify(data),
    })
      .then(json =&gt; {
        // Send info to GTM that purchase was successful
        form.reset()
        window.location.href = `confirmation.html?booking=${json.booking_number}`;
      })
      .catch(error =&gt; {
        console.error('Error: ', error)
        swal('Oops', error.message, 'error');
      })
  }
  </code></pre>