Recent Posts
- (09/10) Fixing Warning: the ECDSA host key for 'github.com' differs from the key for the IP addressTAGS:Web Server Admin
- (12/26) CakePHP 3 - Getting List of Column Definitions from a Table (like schema())TAGS:CakephpCake3
- (09/14) Change Order of Loaded Behaviors in CakePHP 3TAGS:Cake3CakephpWeb ProgrammingPhp
- (05/29) CakePHP 3 - Accept JSON Header Only Working When Debug FalseTAGS:Web ProgrammingCakephpCake3
- (05/23) Remove All Events from Google Calendar (Reset Calendar)TAGS:Web ProgrammingPhp
- (11/08) Google Tag Manager (GTM) Not Firing Default PageView EventTAGS:Web ProgrammingJavascriptGoogle Tag Manager
- (10/13) In PHP, how do you get __toString() Magic Method Result without calling echo?TAGS:CakephpCake3Cakephp 13PhpWeb Programming
- (11/14) Getting output from shell_exec() at all timesTAGS:Web ProgrammingWeb Server Admin
Subscribe to my feed
MainelyDesign.com Blog
Facebook Conversion Tracking On a Click
Posted on 02/10/2014 at 12:36 pm by Kevin Wentworth
Viewed 9,673 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:
- var fb_param = {};
- fb_param.pixel_id = 'xxxxxxxxxxxxx';
- fb_param.value = '0.01';
- fb_param.currency = 'USD';
- (function(){
- var fpw = document.createElement('script');
- fpw.async = true;
- fpw.src = '//connect.facebook.net/en_US/fp.js';
- var ref = document.getElementsByTagName('script')[0];
- ref.parentNode.insertBefore(fpw, ref);
- })();
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.
- var fb_param = {};
- $(document).ready(function() {
- ...
- ...
- // facebook tracking
- $("a.tracked-anchor-id").click(function(e) {
- ...
- })();
- });
Cheers,
-Kevin Wentworth
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!