Are Google Analytics & Adwords ignoring a delayed JavaScript link to the App Store?

My goal is to track conversion rates for my Google Adwords campaign and regular traffic directed to the App Store. Initially, I had a link "/app_store/" on my page that would lead visitors to the app store after a 1-second delay.

I came across a more sophisticated solution using Javascript. For Adwords, it loads a pixel image and for analytics, it triggers a Google Javascript function, briefly pauses, and then redirects to the link.

Unfortunately, this setup isn't yielding results for me. Both Google Analytics and Google Adsense are not registering any visits to the App Store (not even when I test it myself).

<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-18180332-1']);
  _gaq.push(['_trackPageview']);

(function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

function recordOutboundLink(link, category, action) {
    try{
        // Google Analytics
        var pageTracker = _gat._getTracker("UA-18180332-1");
        pageTracker._trackEvent(category, action);

        // Google Adwords
        var image = new Image(1, 1);
        image.src = "http://www.googleadservices.com/pagead/conversion/1046551421/?value=$8&amp;label=zqrfCMWh0QEQ_baE8wM&amp;guid=ON&amp;script=0"
        setTimeout('document.location = "' + link.href + '"', 100)

    } catch(err) {}
}
</script>

For the link:

<a href="http://itunes.apple.com/ae/app/isimplifiedchinese/id377690407?mt=8"
onClick="recordOutboundLink(this, 'Outbound Links', 'http://itunes.apple.com/ae/app/isimplifiedchinese/id377690407?mt=8');return false;">
<img alt="Appstore" src="images/appstore.png"></a>

Any ideas on what might be going wrong here?

Update 23:13 I noticed that with a 100ms delay, I encountered an error message briefly flashing on the screen (took some time to capture in a screenshot).

I only tested this with Erwan's suggestion; unsure if this also occurred in the previous version. The error seems to disappear with longer delays; hence, I increased it to 300ms for precaution.

Answer №1

To ensure proper tracking, make sure to delay the click action (navigating to the link) until it is recorded. Simply add "return false" to the onclick attribute:

onclick="recordOutboundLink(params);return false;"

I trust this information will be beneficial to you.

Answer №2

I was able to successfully make mine work by using _gaq.push(); instead of:

var pageTracker = _gat._getTracker("UA-18180332-1");
pageTracker._trackEvent(category, action);

With the new approach, you can simply do:

_gaq.push(['_trackEvent', category, action]);

This avoids fetching pageTracker again since _gaq was already initialized at the start of the page. It's important to note that Google Analytics does not update in real-time and sometimes takes up to 48 hours to show updated data.

Answer №3

It is conceivable that the script may be initiating a redirect before the GA code has completed its execution. One potential solution could be to place it within the gaq to ensure that it runs after all other code. Your function could then be modified as follows:

function recordOutboundLink(link, category, action) {
    // Google Analytics
    _gaq.push(['_trackEvent', category, action]);

    // Google Adwords
    _gaq.push(function() {
    var image = new Image(1, 1);
    image.src = "http://www.googleadservices.com/pagead/conversion/1046551421/?value=$8&amp;label=zqrfCMWh0QEQ_baE8wM&amp;guid=ON&amp;script=0";
    });
    _gaq.push(function() {setTimeout('document.location = "' + link.href + '"', 100);});
}

Although I have not yet tested this specific implementation, it bears resemblance to methods I have previously attempted.

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

What sets apart Modules in JavaScript from Classes in PHP?

As I dive into online learning resources to understand modules in JavaScript and classes in PHP, it strikes me how alike they are. They both serve the purpose of grouping functions together for more coherent coding practices. In both languages, functions ...

The fetch request became unresponsive and failed to return any data

A ComboBox.js component is interacting with a PHP server. The issue arises in the componentDidMount method, where a fetch call triggers, causing the browser to stall at "http://localhost:3000/search?code=b12a2302e2d0f2b22143b7b4e0472901b2c1b9a8&state=x ...

Methods for showing variables in AJAX

I need assistance with my form that captures 6 numbers from users and saves them as variables upon submission. I am currently utilizing AJAX to show a loading screen. Is there a way to display the entered numbers on this loading screen using the beforeSe ...

Is it possible to extract all parameters, including nested or within arrays, from a Swagger File using Node.js?

Looking for a method to extract and interpret data such as parameters from a Swagger file. Specifically, I am working with the Petstore Swagger API ( ). The definitions within the file contain references to other components, like parameters. One example ...

Move a component within a container based on the position of the mouse cursor upon entry

Is there a way to make an element slide into a div, but have its starting position vary? <div class="block"> <div class="hover">…</div> </div> If the user enters the div from the left, the element should slide in from the le ...

Ways to incorporate a search feature for images without relying on a database

Hey there! I've been working on a code that allows users to search for a book by its name and have only the book with that specific name appear on the page. However, I've run into an issue where the search function doesn't seem to be working ...

Why is it important to incorporate an inner function in order to return to the outer function?

Can you explain the distinction between these two functions, bind_1 and bind_2? function bind_1(f, o) { if (f.bind) return f.bind(o); else return function() { return f.apply(o. arguments); }; } function bind_2(f, o) { if (f. ...

Error message encountered while attempting to upload a file with Express-fileupload in a Node.js environment: "Unable to locate specified

I am currently facing an issue while attempting to upload a PDF file, save it in a directory under a specific name for querying purposes, and store that name in an SQL table. I am utilizing Express-fileupload to handle the file upload process, but when I t ...

The zoom level on Google Maps adjusts based on the size of the window when it

In reference to my previous inquiry about Google maps responsive resize, I am now looking to incorporate dynamic zoom levels based on the window size. Specifically, I want the map to automatically adjust its zoom level when the browser window is resized -- ...

An Ajax call nested within another Ajax call

I've implemented an AJAX request to load my "home" page and "about" page into a designated "container" when a menu link button is clicked on my index.php. Now, I have three links on my "home" page and I want each of these links to open within the same ...

Reactjs will re-render when the this.props.location.query.somequery is updated

When the value of this.props.location.query.someQuery is updated, the render() function will be triggered. However, I am looking for a way to prevent the component from re-rendering when the query is updated. The method I use to update the query is as fo ...

A method to link the chosen state of an option to a boolean variable in AngularJS

My task involves managing a collection of select elements with options specified in a JSON object. Each option should have its selected state preserved as a boolean value. This means that when a user chooses an option, the "selected" value for that option ...

angularjs populating an array using values from another array

(Still learning angularjs) I attempted to populate an array by parsing a CSV file, and it seems like I was successful as everything is correctly stored in the $scope.videos array. However, when I tried to split that array during page load using the init( ...

Find the most recent date in a file and display the line associated with it

I am working with a document named Application.txt that consists of multiple columns and rows as shown below: ApplNo DocsURL DocDate 4782 www…. 7/28/2003 4782 www…. 11/23/2008 4782 www…. 3/24/2012 5010 www…. 4/5/2003 5010 ww ...

Looking to disable the row highlighting once a new row is selected?

Is there a way to remove the row highlight when clicking on another table row? Currently, I'm encountering an issue where the table row remains highlighted even after clicking on a different row or gear icon. Does anyone have suggestions on how to ad ...

Issues with AJAX causing MYSQL to get NULL data inserted

My HTML code collects latitude and longitude coordinates and sends them to a PHP script using AJAX. However, the issue is that the AJAX request inserts NULL values into the database table. It seems to be inserting "0.000000" for both Latitude and Longitude ...

Shade the div if the name of the child element matches the location's hash

I am currently working on an AngularJS web project. My goal is to highlight a specific div when clicking on an anchor link. The structure of the HTML is as follows: <div interaction-list-item="" sfinx-interaction="interaction" class="ng-isolate-scope" ...

Tips for successfully implementing an Ocrad.js example

I've been working on an OCR (optical character recognition) web application and stumbled upon the Ocrad.js JavaScript library Ocrad.js. It seems like a perfect fit for what I need, but unfortunately, I'm having trouble getting it to function prop ...

I am opting to choose children using JavaScript instead of jQuery due to limitations in my current project

Currently, I am utilizing jQuery to determine if there exists an li element within the #mainNavigation div that possesses the data-val attribute of "-1". This functionality is easily achievable with jQuery, however, I am in need of replicating the same b ...

Converting a JavaScript map into an array with ES6: A step-by-step guide

I am in need of a solution to convert a JavaScript map into an array with a specific format: [ { name: "21 years old", totalUsers: 2, }, ... ] For example, if I have the following map: const ages = { "21": 2, "18 ...