Skip to content

Instantly share code, notes, and snippets.

@acoyfellow
Last active March 30, 2017 00:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acoyfellow/ed8d4bb18e57c0542792 to your computer and use it in GitHub Desktop.
Save acoyfellow/ed8d4bb18e57c0542792 to your computer and use it in GitHub Desktop.
How to fire a Facebook Custom Audience pixel after 30 seconds with Javascript
/*
****** A normal Facebook retargeting pixel looks like this:
__________________________________________________________________________
__________________________________________________________________________
<script>(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;
}
_fbq.push(['addPixelId', '****']);
})();
window._fbq = window._fbq || [];
window._fbq.push(['track', 'PixelInitialized', {}]);
</script>
<noscript><img height="1" width="1" alt="" style="display:none" src="https://www.facebook.com/tr?id=****&amp;ev=PixelInitialized" /></noscript>
__________________________________________________________________________
__________________________________________________________________________
****** To make it fire after 30 seconds, use this code instead:
__________________________________________________________________________
__________________________________________________________________________
<script>
setTimeout(function(){ // The setTimeout javascript function will help you fire the JS code after a set amount of time
(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;
}
_fbq.push(['addPixelId', '****']);
})();
window._fbq = window._fbq || [];
window._fbq.push(['track', 'PixelInitialized', {}]);
}, 30000); // 30000 = 30 seconds
</script>
<noscript><img height="1" width="1" alt="" style="display:none" src="https://www.facebook.com/tr?id=****&amp;ev=PixelInitialized" /></noscript>
__________________________________________________________________________
__________________________________________________________________________
Be sure to replace "****" with your ID
** Need help? Contact fb.com/coeyman || Jordan@SendGrowth.com
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment