Search code examples
javascriptgoogle-ads-apiconversion-tracking

Conversion Tracking After Specific Time


I want to set up a conversion metric to track when a user has been on a page longer than 30 seconds. I'm confused about Google Adwords. It's provided me with a piece of javascript code to inject into my body tag. Here's my problem. There is no obvious piece of code that I see that actually initiates the conversion. Look below:

<!-- Google Code for 30+ Seconds on site Conversion Page -->
<script type="text/javascript">
/* <![CDATA[ */
  var google_conversion_id = "my id here";
  var google_conversion_language = "en";
  var google_conversion_format = "3";
  var google_conversion_color = "ffffff";
  var google_conversion_label = "the label";
  var google_remarketing_only = false;
/* ]]> */
</script>

<script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js">
</script>

My plan is to set up a simple timeout delay in javascript and once those 30 seconds have past, then run the code above. But will that work? I don't see any line that shouts "Hey, this line of code communicates to the google server and initiates this conversion". What do I need to do get this to work? Thanks.


Solution

  • The problem is, this code doesn't really "run" anything, it just tells the browser to load a script.

    To add some delay in that, you need to create the JS element and inject it later in your page.

    You could try something like that:

    <script type="text/javascript">
      var google_conversion_id = "my id here";
      var google_conversion_language = "en";
      var google_conversion_format = "3";
      var google_conversion_color = "ffffff";
      var google_conversion_label = "the label";
      var google_remarketing_only = false;
    
      var head = document.getElementsByTagName('head').item(0);
      var script = document.createElement('script');
      script.setAttribute('type', 'text/javascript');
      script.setAttribute('src', '//www.googleadservices.com/pagead/conversion.js');
    
      setTimeout(function() {
        head.appendChild(script);
      }, 30000);
    </script>
    

    Or you could just use the asynchronous version of this ;-)