Home > Facebook & Instagram >

Super Simple Javascript for Facebook Tracking (16)


04-30-2015 08:47 AM #1 zeno (Administrator)
Super Simple Javascript for Facebook Tracking

This is quite belated, my bad, but important to know!

One thing I advocate is using Facebook's conversion tracking whenever possible.

It's just smart. It's a core part of their ad delivery systems when using oCPM, makes your life easier if not using a PMD, and gives you an edge that lazy affiliates wont have.

In a nutshell: if you bid oCPM, you should send Facebook conversion events from the deepest part of your funnel that you can get to. For many of you, especially if running mobile apps, this will be the click through event on your lander.

Let me ask you this: if you had the choice, would you want Facebook to optimise your ad delivery over time to people who click through your lander, or do nothing at all? Common sense dictates that the clickthrough, a quite clear and deliberate declaration of interest/intent by the user, indicates that the user is more interested in your product and therefore more likely to convert.

So, let's feed that data to Facebook. It's actually very easy. Even a honey badger could do it.

Step 1:

Paste this code on your landing page right before the closing body tag (</body>):

Code:
<script>
var fbLoaded = false,
        loadFB = function () {
            (function() {
                var _fbq = window._fbq || (window._fbq = []);
                if (!_fbq.loaded) {
                    var fbds = document.createElement('script');
                    fbds.async = true;
                    fbds.src = '//connect.facebook.net/en_US/fbds.js';
                    var s = document.getElementsByTagName('script')[0];
                    s.parentNode.insertBefore(fbds, s);
                    _fbq.loaded = true;
                }
            })();
            window._fbq = window._fbq || [];
            window._fbq.push(['track', 'ACCOUNT_NUMBER', {'value':'0.00','currency':'USD'}]);
        };
</script>
Now, remember to replace ACCOUNT_NUMBER with that for your FB account - just look it up by finding the conversion pixel your made (they are different numbers for each one you make), and change the currency to suit + the value to the conversion value, e.g. 2.50 for $2.50 USD.

Also note the line: fbds.src = '//connect.facebook.net/en_US/fbds.js'; -- the // part here means it is protocol relative, so that link adopts whatever protocol your browser is using - http or https. If you are viewing your lander locally the protocol is file:// and it won't load the FB script properly. So for local testing, add http: to that link. You can then see things work in your web console. If this makes no sense to you, don't worry.



Step 2:

We want this script to load and trigger a conversion ONLY when a user clicks our CTA. So, we need to call this script when that happens - BUT, if people click the link it will start loading another page and this may interrupt the script loading, which we don't want.

To work around this I would recommend making your CTA open in a new window. You can split-test doing this and not. When it pops up in a new window on a mobile device the animation might actually make users feel like it took less time to load. On the other hand if their phone is slow it may take longer! Either way, I suggest you start by making your CTA look like this:

Code:
<a href='#' class='something' onclick="loadFB()" target="_blank">Call To Action</a>
And there you have it!

So, for an existing lander, all you would have to do is paste in the first code, then add onclick="loadFB()" target="_blank" to the <a> element.

Make sure you upload the page somewhere, open your browser's web console and test this. When you click the link you should see that this triggers a FB URL to load in the console.



Enjoy!


04-30-2015 10:23 AM #2 caurmen (Administrator)

This is awesome - thanks, Zeno.

If you don't want to push people to a new window, I think you could also run this code as an intermediate step after they click on the lander. In other words, you'd have a page that they went to after clicking on the lander which would just run that code then redirect to the offer.

Am I correct in that, or are there FB-specific reasons that wouldn't play?


04-30-2015 12:08 PM #3 zeno (Administrator)

The main disadvantage with that is that you would need to use a timed redirect to ensure ample time for Facebook's conversion tracking code to load.

But what's reasonable? 500 ms? 1000 ms? 2000 ms? It will depend on a users connectivity and device, and in the end this all presents more 'blank space' wait time to the user. You could have an animation which says 'loading' or similar but in the end they are getting from A to B slower.

With the new window approach you at least leave the window loading the JS behind and open so that you don't need to wait on moving users through to the offer. The only thing I can think of that might pose a problem would be if the browser is slow, doesn't load the JS, and then pauses JS processing when the tab moves into the background. I'm not sure if this happens. Even if it did, the script might then complete loading when the user goes back to that tab. It's probably a negligible issue.


05-05-2015 05:36 AM #4 zeno (Administrator)

This was originally a little nugget for the 6WAMC members. Since it's now featured in the newsletter, it's live for all!

It's very simple, but likely overlooked by a lot of advertisers.


05-05-2015 10:57 AM #5 h0mp (Member)

Would it be wise to use a conversion pixel on cloaked campaigns? It exposes stuff, i think? If it's not wise to put up the pixel.. you should put a disclaimer


05-09-2015 11:07 AM #6 zeno (Administrator)

It's potentially unwise - when firing a javascript event by calling on an external script then the server hosting the script can definitely see the referring page. You can work around it with some know-how.

If I'm frank... if people want to cloak on Facebook then they should know all the caveats/risks themselves, I'd rather not accommodate that audience when providing tutorials/guides too much (plus it always leads more and more newbies down that path whenever you mention this 'cloaking' thing they haven't heard of).


05-12-2015 03:46 AM #7 Mr Yaz (Member)

This is a great K-bomb Zeno. Thanks for the pro-tip!


05-17-2015 11:24 AM #8 visitorstoleads (Member)

Hey zeno, need to quickly jump in here.

First of all, this trick is golden. I have just another method to implement it and I wanted to ask if you see any problems with that.

I recently started to use Google Tag Manager on ALL of my sites (blogs, landingpages, client sites, etc.) to implement all my code snippets (GA, Facebook WCA, etc.) and I would never go back, if you haven't checked it out, please do now.

With Google Tag Manager, I created a link click listener and set up a trigger and tag that works this way: IF a user clicks on an outgoing link on my landingpage (the rule was if link click url = trackinglink.com) THEN fire a Facebook Conversion Pixel (easy to implement with GTM in a few clicks WITHOUT having to change the code).

To prevent the pixel loading time issues, all my outgoing offer links are target=blank anyway so shouldn't be a problem there.

Anyone sees any disadvantages with that technique?

PS: The guy in this video describes a similar technique, he is firing fb conversion pixels when someone clicks on a valid form submit: https://www.youtube.com/watch?v=mMjeXKICUGY


05-17-2015 11:16 PM #9 adollf (Member)

I have multiple facebook accounts, but only one landing where the pixels fire. Do i have to do one landing page copy each pixel or can i implement 10-20 on the same lp?


05-19-2015 07:36 AM #10 zeno (Administrator)

Quote Originally Posted by visitorstoleads View Post
Hey zeno, need to quickly jump in here.

First of all, this trick is golden. I have just another method to implement it and I wanted to ask if you see any problems with that.

I recently started to use Google Tag Manager on ALL of my sites (blogs, landingpages, client sites, etc.) to implement all my code snippets (GA, Facebook WCA, etc.) and I would never go back, if you haven't checked it out, please do now.

With Google Tag Manager, I created a link click listener and set up a trigger and tag that works this way: IF a user clicks on an outgoing link on my landingpage (the rule was if link click url = trackinglink.com) THEN fire a Facebook Conversion Pixel (easy to implement with GTM in a few clicks WITHOUT having to change the code).

To prevent the pixel loading time issues, all my outgoing offer links are target=blank anyway so shouldn't be a problem there.

Anyone sees any disadvantages with that technique?

PS: The guy in this video describes a similar technique, he is firing fb conversion pixels when someone clicks on a valid form submit: https://www.youtube.com/watch?v=mMjeXKICUGY

This kind of thing is great for automation and when using a large array of sites/platforms. Personally, I don't like the idea of requesting content from Google just to serve code that I could paste in myself using snippets or dynamic injection with PHP.

Disadvantage: potential speed impact, Google can see requests from your pages, personally I don't like the Google Tag Manager interface.
Advantage: convenience and ability to update site(s)-wide when needed.


Quote Originally Posted by adollf View Post
I have multiple facebook accounts, but only one landing where the pixels fire. Do i have to do one landing page copy each pixel or can i implement 10-20 on the same lp?
Pixels are account-based so you will need to do the right pixel for the right account sending traffic.

Putting 10-20 on the same page is an absolutely terrible idea.

The easy way is to make a different page per account (and domain). Or use dynamic injection with PHP.

Frankly, if you are running 10-20 accounts and are sending traffic from all of them to the same page... you are leaving a giant footprint that will get you burned pretty quickly.


05-20-2015 08:18 PM #11 julien (Member)

Hey Zeno, if a landing page is using jQuery, I think it's a better solution to use
https://api.jquery.com/ready/
or
https://api.jquery.com/load-event/
to fully load your pixel before redirecting don't you think?

Of course, it can be used only if you agreed to lose a bit of performance with the load of jQuery in the first place.


05-28-2015 01:09 PM #12 zeno (Administrator)

As far as I know those could be used to control script loading when you enter the page.

But when you click to exit, you would probably have to use something else, e.g. a custom script that loads the FB conversion code, waits for something, then does a JS redirect to the tracking link. This is elegant but again there is the issue of having to wait for the FB conversion script to load, which could take 200-2000 ms depending on the user.

I might test that approach though - something to split-test between landers :-)


07-23-2015 08:50 PM #13 fatcow (Member)

Hi Zeno,

I tested a lot around this and just can't get this to work proberly.. The pixel fires unreliably and the discrepancies are often huge between adsets (on a few it seems to work with the "offer clickers" but on most adsets its either way too few or too many)...

Any idea what we are doing wrong? Maybe using the new Updated Custom Audience Pixels are more accurate? Did you test mainly on mobile or also on desktop?

For now, we are seeing a little more accurate results with the help of Google Tag Manager, but also there it's no where near 90-100% accuracy to what eg. Voluum Shows...

Thank you for any good ideas on how to troubleshoot this more!


09-17-2015 07:14 AM #14 zeno (Administrator)

Hmm interesting.

The new custom audience tracking is for sure the way to go - they will deprecate the old style, and the JS they are using may now be faster.

I don't know if there is a way to make this 100% reliable with an onClick event but I will check it out. Your best bet is to make the CTA open in a new tab so that the original page can load without interruption, or use an intermediate lander redirect.


09-18-2015 04:33 AM #15 northernlights (Member)

One rule of thumb that has served me well is that never put anything that creates money directly in tag manager, always either inline js or from separate file. The biggest reason is not actually performance but blockers like Ghostery which will block all Googles JS spyware including tag manager. So use tag manager only for common scripts like analytics etc.


09-18-2015 05:37 AM #16 zeno (Administrator)

Oh, very useful advice. Take note everyone - that is actually quite important to realise and will only grow as a problem.


Home > Facebook & Instagram >