Home > Technical & Creative Skills > Programming, Servers & Scripts

Place Facebook pixel from URL parameter. (4)


11-08-2015 09:50 PM #1 gb27 (Member)
Place Facebook pixel from URL parameter.

Hey guys, i wonder if someone could help me out with this.

Im trying to pass a facebook pixel dynamically form the url.

So basically i want to have http://example.com?pixel=123456789

Then the {pixel} below wll be replaced with the pixel in the code below.


Thanks


<!-- Facebook CONVERSION PIXEL -->
<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;
}
})();
window._fbq = window._fbq || [];
window._fbq.push(['track', '{PIXEL}', {'value':'0.00','currency':'EUR'}]);
</script>
<noscript><img height="1" width="1" alt="" style="display:none" src="https://www.facebook.com/tr?ev={PIXEL}&amp;cd[value]=0.00&amp;cd[currency]=EUR&amp;noscript=1" /></noscript>


11-14-2015 04:43 PM #2 leewardbound (Member)

Easy, php is designed to handle string insertion like this, and getting the variable from the URL is super simple... just change your above script like this - replace {pixel} with <?= $_GET['pixel'] ?>

I'm on my mobile or I'd do it for you, hit me @leeward.bound on skype if you need help implementing


11-14-2015 05:24 PM #3 gb27 (Member)

Oh awesome thanks dude. I will try that and add you on Skype as well.


11-16-2015 03:10 AM #4 bobliu (Member)

Did you work it out ok? Make sure the file in the root of your site is called/re 'index.php'

Code:
<!-- Facebook CONVERSION PIXEL -->
<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;
}
})();
window._fbq = window._fbq || [];
window._fbq.push(['track', '<?php=$_GET['pixel'];?>', {'value':'0.00','currency':'EUR'}]);
</script>
<noscript><img height="1" width="1" alt="" style="display:none" src="https://www.facebook.com/tr?ev=<?php=$_GET['pixel'];?>&amp;cd[value]=0.00&amp;cd[currency]=EUR&amp;noscript=1" /></noscript>
It can also be done without a PHP script (pure JS) that will be a lot faster when dealing with tones of traffic (less server load). Let me know if you want me to hack something up for that.


Home > Technical & Creative Skills > Programming, Servers & Scripts