-
AuthorPosts
-
March 1, 2016 at 12:06 am #817841
Hi Theme-X!
I tried to add facebook pixel code to my child themes function file but it returned syntax error. Any idea why? See code below.
// Add Facebook custom audience pixel Code
// =============================================================================
function add_facebook_audience_pixel_code(){
?><!– Insert Facebook custom audience pixel Code starts here –>
<!– Facebook Pixel Code –>
<script>
!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
n.push=n;n.loaded=!0;n.version=’2.0′;n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
document,’script’,’//connect.facebook.net/en_US/fbevents.js’);fbq(‘init’, ‘1527047177599824’);
fbq(‘track’, “PageView”);</script>
<noscript><img height=”1″ width=”1″ style=”display:none”
src=”https://www.facebook.com/tr?id=1527047177599824&ev=PageView&noscript=1″
/></noscript>
<!– End Facebook Pixel Code –><!– Insert Facebook custom audience pixel Code Code ends here –>
<?php
}”
add_action( ‘wp_head’, ‘add_facebook_audience_pixel_code’ );
// End Facebook custom audience pixel Code
// =============================================================================March 1, 2016 at 1:45 am #817969Hi there,
Please update your code to :
// Add Facebook custom audience pixel Code // ============================================================================= function add_facebook_audience_pixel_code(){ ?> <!– Insert Facebook custom audience pixel Code starts here –> <!– Facebook Pixel Code –> <script> !function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod? n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n; n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0; t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window, document,'script','//connect.facebook.net/en_US/fbevents.js'); fbq('init', '1527047177599824'); fbq('track', "PageView");</script> <noscript><img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=1527047177599824&ev=PageView&noscript=1" /></noscript> <!– End Facebook Pixel Code –> <!– Insert Facebook custom audience pixel Code Code ends here –> <?php } add_action( 'wp_head', 'add_facebook_audience_pixel_code' ); // End Facebook custom audience pixel Code // =============================================================================
Hope it helps.
March 5, 2016 at 11:52 am #824892Yes, that worked!
Now when I have the pixel on all pages, how can I add one event pixel to a specific page?
I’m looking to add:
fbq(‘track’, ‘Lead’);
March 5, 2016 at 7:23 pm #825188Hello There,
Please update the code and use this instead:
// Add Facebook custom audience pixel Code // ============================================================================= function add_facebook_audience_pixel_code(){ ?> <!– Insert Facebook custom audience pixel Code starts here –> <!– Facebook Pixel Code –> <script> !function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod? n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n; n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0; t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window, document,'script','//connect.facebook.net/en_US/fbevents.js'); fbq('init', '1527047177599824'); fbq('track', "PageView"); <?php if ( is_page( 'typ-scroll-box' ) ) : ?> fbq('track', 'Lead'); <?php endif; ?> </script> <noscript><img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=1527047177599824&ev=PageView&noscript=1" /></noscript> <!– End Facebook Pixel Code –> <!– Insert Facebook custom audience pixel Code Code ends here –> <?php } add_action( 'wp_head', 'add_facebook_audience_pixel_code' ); // End Facebook custom audience pixel Code // =============================================================================
Please let us know if this works out for you.
March 9, 2016 at 9:52 pm #831425Thanks! Yes it worked!
If I’d like to add an additional events pixel to three other pages, can I just add it after fbq(‘track’, ‘Lead’);?
I’m looking to add fbq(‘track’, ‘ViewContent’); to
http://kamranazadmd.com/blog/breast-implants-is-bigger-always-better/
http://kamranazadmd.com/blog/tummy-tuck-abdominoplasty-top-concerns-video/
http://kamranazadmd.com/contact-us/Thanks!!
March 9, 2016 at 11:19 pm #831541Hi there,
Sure, please change the above code to this.
// Add Facebook custom audience pixel Code // ============================================================================= function add_facebook_audience_pixel_code(){ ?> <!– Insert Facebook custom audience pixel Code starts here –> <!– Facebook Pixel Code –> <script> !function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod? n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n; n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0; t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window, document,'script','//connect.facebook.net/en_US/fbevents.js'); fbq('init', '1527047177599824'); fbq('track', "PageView"); <?php switch ( get_the_ID() ) { case 1723: echo "fbq('track', 'Lead');"; break; case 2227: case 2123: case 28: echo "fbq('track', 'ViewContent');"; break; } ?> </script> <noscript><img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=1527047177599824&ev=PageView&noscript=1" /></noscript> <!– End Facebook Pixel Code –> <!– Insert Facebook custom audience pixel Code Code ends here –> <?php } add_action( 'wp_head', 'add_facebook_audience_pixel_code' ); // End Facebook custom audience pixel Code // =============================================================================
Instead of adding and checking the page name, let’s just switch over their post/page IDs.
Hope this helps.
March 11, 2016 at 10:00 am #833723Fantastic!!
That worked! Just curious, you have ‘echo’ after the page number, I’m using integrity, does that matter? Or maybe that’s for something else and not the stack?
Thanks again!!
March 11, 2016 at 3:34 pm #834078Hi there,
Glad it worked! It should be okay and not specific to stack. I just use echo to make it cleaner. It’s the same as this,
case 1723: ?> fbq('track', 'Lead'); break; <?php
But it’s better to have a single echo.
Cheers!
-
AuthorPosts