Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are 2 elements here: First, DOM ready with jQuery. That's easy:</p> <pre><code>$(document).ready(function(){ //do something }); </code></pre> <p>The way to ensure that your function executes <em>after</em> Google Analytics has created its cookies (once it had implicitly executed <code>initData()</code>) is to pass the function within the <code>_gaq</code> array queue so that it gets executed after the <code>_trackPageview</code> call. </p> <p>For example:</p> <pre><code>_gaq.push(["_trackPageview"]); _gaq.push(function(){ //when this executes, the google analytics cookies definitely exist }); </code></pre> <p>So, you could put the following anywhere on the page below <code>_gaq.push(["_trackPageview"])</code> (or after any <code>_gaq</code> call that initiates the cookies; most commonly, <code>_trackPageview</code>, <code>_trackEvent</code>, and <code>_setCustomVar</code>. </p> <pre><code>_gaq.push(function(){ //do something }); </code></pre> <p>Now to ensure that the function <em>both</em> executes after DOM Ready <em>and</em> that <code>initData()</code> is implicitly called, you can either do this:</p> <pre><code>_gaq.push(function(){ $(document).ready(function(){ //do something }); }); </code></pre> <p>Or:</p> <pre><code>$(document).ready(function(){ _gaq.push(function(){ //do something }); }); </code></pre> <p>They both do what you're looking for, though it sounds like the first one better fits your mental model of what you're trying to accomplish.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload