Navigation
This is archived content. Visit our new forum.
  • Author
    Posts
  • #817841

    kamranazad
    Participant

    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
    // =============================================================================

    #817969

    Christopher
    Moderator

    Hi 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.

    #824892

    kamranazad
    Participant

    Yes, 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’);

    – to http://kamranazadmd.com/typ-scroll-box/

    #825188

    Rue Nel
    Moderator

    Hello 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.

    #831425

    kamranazad
    Participant

    Thanks! 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!!

    #831541

    Rad
    Moderator

    Hi 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.

    #833723

    kamranazad
    Participant

    Fantastic!!

    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!!

    #834078

    Rad
    Moderator

    Hi 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!