Troubleshooting Issue with Google Analytics Event Sending upon Ajax Success Failure

I've created a Process Transaction Knockout method that retrieves a transaction status. Depending on this status, I want to trigger an event in analytics.js. Despite using analytics-debug.js and seeing "Send finished" in the console, the event never appears in Google Analytics.

The send event works if placed before or after the ajax request, but not within the success or done callbacks.

$.ajax({
    url: '/ProcessTransaction',
    type: 'post',
    data: 'data',
    contentType: 'application/json',
    success: function (result) {
        if (result.StatusCode == 1 || result.StatusCode == 4) {
            var subtotal = 7.95;
            var enrollmentFee = 25;
            var total = subtotal + enrollmentFee;
            ga('ec:addProduct', {
                'id': 'P12345',
                'name': 'Test Product 1',
                'category': 'Products',
                'brand': 'Brand',
                'price': subtotal,
                'quantity': 1
            });
            ga('ec:setAction', 'purchase', {
                'id': 'T12345',
                'affiliation': 'TestSite',
                'revenue': total,
                'tax': enrollmentFee,
            });
            ga('send', 'event', 'Review', 'transaction', 'approved', total);
            $('#submitpaymentbtn').data('loading-text', 'Approved!').button('loading');
            window.location.href = '@Url.Action("Confirmation", new { ParticipantId = Participant.ParticipantId })';
        }
        else {
            $('#modal-error').modal();
        }
    }
});

Answer №1

When sending an event, make sure to use whole numbers for the value. If decimals are present, simply round the total using Math.round(). Even if the analytics debug indicates that the event was sent successfully, it will not be accepted or logged if the value is not an integer.

ga('send', 'event', 'Review', 'transaction', 'approved', Math.round(total));

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Updating template views in Grails without the use of ajax enables seamless and dynamic

In my GSP template, I am looking to provide the user with updates on the progress of a task within an overlay. Once the user submits their data, the controller triggers a service to carry out the task. Simultaneously, another service calculates the percen ...

Make sure to always select the alternative option in ajax

I am trying to create a condition where if the value of id=type_investor is either 1 or 6, an email should be sent using a different controller. Here is my complete code: function (isConfirm) { if (!isConfirm) return; $.ajax({ ...

Contrast between gmaps and traditional cross-domain AJAX queries

I am curious about the behavior of the getJSON method when making an ajax call to specific addresses. For example, when I make a call to "", no errors are generated. However, if I try to call an external domain that is not Google, the browser throws an e ...

Retrieve the total number of clicks and initiate a single AJAX request using jQuery

When a user continuously clicks a link, I need to call an ajax service. Currently, my code only works for a single click - the ajax call is made and functions correctly, but if the user clicks multiple times, only the first click is registered. I want to k ...

KnockoutJS - Using containerless control flow binding with predefined values

Inside a select control, I am using ko:foreach instead of the usual bindings. Everything is working perfectly, except that the initial value for "specialProperty" is set to unknown even when the select control is set to Option 1. It behaves as expected o ...

Transferring data from a PhoneGap application to an XLS file and attaching it to an email for easy sharing

Is there any example available that demonstrates how to achieve the following functionality in Phonegap? My app retrieves data through AJAX from our server. I would like users to be able to export these results to an Excel spreadsheet which can then be at ...

Is it possible to save an entire webpage that infinitely scrolls without actually manually scrolling through it?

I'm dealing with a webpage that has infinite downward scrolling. I tried automating the scrolling, but eventually the page became too large to continue scrolling. To fix this, I manually removed some DIV blocks from the source code which decreased the ...

Setting up your YAML configuration to utilize both PHP and Python with AJAX in a unified project on App Engine

Here is my project idea https://i.stack.imgur.com/oGOam.jpg $.ajax({ url: '../server/python/python_file.py', dataType: 'json', type: 'POST', success:function(data) { //Perform additional AJAX requests using PHP f ...

Unable to execute response

I'm facing an issue with a form in which I am utilizing jquery.ajax to execute PHP code in the background. Unfortunately, my JavaScript code isn't handling the response from the script properly. Interestingly, if I use if (status == "success"), ...

Load HTML table values dynamically with Ajax post page load in PHP

My goal is to retrieve the connectivity status of available servers in a database on a PHP page. <tbody> <?php foreach ($data['servers'] as $server) { ?> <tr> <td class=""><?php echo $server->server_ ...

Converting a JSON object into a format suitable for transmission through AJAX requests

I am attempting to transmit data in a JSobject via AJAX using jQuery. Below is the json object: var cookieData = { 'land' : document.URL, 'ref' : document.referrer }; The object is then stored in a cookie... throu ...

The execution of dynamically generated Javascript in JSON format, returned through AJAX, is prevented when it is appended

Recently, I encountered an issue on my webpage where I made an AJAX request to a PHP script. The PHP script responded with a valid JSON object and set the Content-type header to application/json. After examining the JSON format using console.log(JSON.stri ...

Is there a different option than jQuery.ajaxSettings.traditional for use with XMLHttpRequest?

Is there a different option similar to jQuery.ajaxSettings.traditional for xmlhttprequest? or What is the method to serialize query parameters for an xmlhttprequest? ...

Looping through a series of elements and performing jQuery Ajax requests

I am facing an issue with retrieving values in the success function (ajax) within the $.each loop. This is what I have: var test = []; $.each(images, function(index){ var formData = new FormData(); formData.append('image', images[index] ...

AngularJS: Ensuring Controller Functions are Executed Post AJAX Call Completion via Service

Here's the code snippet I have in my service. this.loginUser = function(checkUser) { Parse.User.logIn(checkUser.username, checkUser.password, { success: function(user) { $rootScope.$apply(function (){ $rootScop ...

Troubleshooting Problems with Ruby Arrays, JavaScript, and JSON

I am facing a challenge with rendering a highcharts plugin in my rails application. I suspect it could be related to the sql queries fetching data from the database and converting them into a ruby array that the javascript code fails to interpret correctly ...

send document through ajax

Having some trouble with this task. Here is what I've managed to put together so far: <input id="newFile" type="file"/> <span style="background-color:orange;" onClick="newImage()">HEYTRY</span> I know it's not much progress. ...

retrieve the current image source URL using JavaScript

In the template below, I am looking to extract the current img src URL and utilize it in a fancybox button. For example, in the template provided, there are 3 images from https://farm6.staticflickr.com. When clicking on these images, the fancybox will ope ...

Tips for effectively managing the timeout of server-side AJAX calls1. Maxim

I'm currently developing server code in ASP.NET with the MVC framework. The client-side code is written in javascript. When sending an AJAX request from the browser to the server, whether using JQuery or not, timeouts can be set. Additionally, the br ...

When executing store.sync() in ExtJS, all fields are passed

In the latest version of ExtJS (6.5.0), I have set up a Store and an editable grid panel: Ext.define('StateStore',{ extend: 'Ext.data.Store', alias: 'store.stateStore', storeId : 'StateStore', field ...