MainelyDesign.com Blog

Facebook Conversion Tracking On a Click

Posted on 02/10/2014 at 12:36 pm by Kevin Wentworth
Viewed 6,890 times | 0 comments

Be Cautious Using Conversion Tracking Scripts Supplied by Vendors

We recently experienced a small challenge with the Facebook Conversion Tracking script.  We were using the standard boiler plate script as provided to developers by facebook.  What's touted as a simple method to integrate conversion tracking may not work if you actually want to track on a click action, instead of on a page load.  If you are actually using any JavaScript (particularly jQuery) on your site prior to adding the Facebook Conversion Tracking code, you might not track any conversions.

Tracking Facebook Conversions on Click

Simple answer: make sure var fb_param; is in the GLOBAL variable scope.

Long answer: see below.

This is the common script is supplied by Facebook:

  1. var fb_param = {};
  2. fb_param.pixel_id = 'xxxxxxxxxxxxx';
  3. fb_param.value = '0.01';
  4. fb_param.currency = 'USD';
  5. (function(){
  6. var fpw = document.createElement('script');
  7. fpw.async = true;
  8. fpw.src = '//connect.facebook.net/en_US/fp.js';
  9. var ref = document.getElementsByTagName('script')[0];
  10. ref.parentNode.insertBefore(fpw, ref);
  11. })();

Making Facebook Conversion Tracking Play Nice

In our cakephp driven sites, we determined (only after some rigorous debugging) that the fb_param array was undefined in the click handler. In this instance, the problem was easily resolved by relocating the var fb_params; declaration up to the global-scope level.

We placed var fb_param = {}; outside of the  "$(document).ready(function() {"  thus making it globally available.

  1. var fb_param = {};
  2. $(document).ready(function() {
  3. ...
  4. ...
  5. // facebook tracking
  6.     $("a.tracked-anchor-id").click(function(e) {
  7.        ...
  8.         })();
  9.     });

Cheers,
-Kevin Wentworth

Bookmark and Share

Tags for Facebook Conversion Tracking On a Click

Jquery | Web Programming | Usage

Comments for this Posting

No comments. Be the first to post a reply.

Sorry, comments are closed for this posting.

Please Email Kevin if you have any questions. Thanks!

Meet Site Avenger - Hosted Content Management System

Powered By: Site Avenger | Site Production: Saco Design