set up Facebook Pixel only for production in ReactJS












0















I am using React as my front-end (and Rails as my back-end).



I am setting up the Facebook Pixel Events Manager to track my Facebook ads on my website.



So I added this script to my public/index.html:



<!-- 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',
'https://connect.facebook.net/en_US/fbevents.js');
fbq('init', 'MYID');
fbq('track', 'PageView');
</script>
<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=MYID&ev=PageView&noscript=1"
/></noscript>
<!-- End Facebook Pixel Code -->


and in my React Components I added this callback function anytime a button is clicked and raise an event:



() => fbq('track', 'MyEvent');



It seems to work, although I need my pixels to fire only in production. I can't access process.env.NODE_ENV in public/index.html.



What can I do to have this code used only in production ?










share|improve this question


















  • 1





    That file is delivered as is. To add a pixel only in production you'd need to have a separate index file you use in the production build, or add that code into a component that has access to the env, like in your footer component.

    – Dvid Silva
    Jan 2 at 17:35











  • thank you for your help @Dvid Silva ! OK so I have a component that wraps my entire app; Should I put this code in my ComponentDidMount() method ? If I want to use a separate index file, how can I do that ? create another entry point ?

    – Jules Corb
    Jan 3 at 8:26
















0















I am using React as my front-end (and Rails as my back-end).



I am setting up the Facebook Pixel Events Manager to track my Facebook ads on my website.



So I added this script to my public/index.html:



<!-- 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',
'https://connect.facebook.net/en_US/fbevents.js');
fbq('init', 'MYID');
fbq('track', 'PageView');
</script>
<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=MYID&ev=PageView&noscript=1"
/></noscript>
<!-- End Facebook Pixel Code -->


and in my React Components I added this callback function anytime a button is clicked and raise an event:



() => fbq('track', 'MyEvent');



It seems to work, although I need my pixels to fire only in production. I can't access process.env.NODE_ENV in public/index.html.



What can I do to have this code used only in production ?










share|improve this question


















  • 1





    That file is delivered as is. To add a pixel only in production you'd need to have a separate index file you use in the production build, or add that code into a component that has access to the env, like in your footer component.

    – Dvid Silva
    Jan 2 at 17:35











  • thank you for your help @Dvid Silva ! OK so I have a component that wraps my entire app; Should I put this code in my ComponentDidMount() method ? If I want to use a separate index file, how can I do that ? create another entry point ?

    – Jules Corb
    Jan 3 at 8:26














0












0








0








I am using React as my front-end (and Rails as my back-end).



I am setting up the Facebook Pixel Events Manager to track my Facebook ads on my website.



So I added this script to my public/index.html:



<!-- 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',
'https://connect.facebook.net/en_US/fbevents.js');
fbq('init', 'MYID');
fbq('track', 'PageView');
</script>
<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=MYID&ev=PageView&noscript=1"
/></noscript>
<!-- End Facebook Pixel Code -->


and in my React Components I added this callback function anytime a button is clicked and raise an event:



() => fbq('track', 'MyEvent');



It seems to work, although I need my pixels to fire only in production. I can't access process.env.NODE_ENV in public/index.html.



What can I do to have this code used only in production ?










share|improve this question














I am using React as my front-end (and Rails as my back-end).



I am setting up the Facebook Pixel Events Manager to track my Facebook ads on my website.



So I added this script to my public/index.html:



<!-- 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',
'https://connect.facebook.net/en_US/fbevents.js');
fbq('init', 'MYID');
fbq('track', 'PageView');
</script>
<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=MYID&ev=PageView&noscript=1"
/></noscript>
<!-- End Facebook Pixel Code -->


and in my React Components I added this callback function anytime a button is clicked and raise an event:



() => fbq('track', 'MyEvent');



It seems to work, although I need my pixels to fire only in production. I can't access process.env.NODE_ENV in public/index.html.



What can I do to have this code used only in production ?







reactjs facebook environment-variables






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 16:42









Jules CorbJules Corb

494515




494515








  • 1





    That file is delivered as is. To add a pixel only in production you'd need to have a separate index file you use in the production build, or add that code into a component that has access to the env, like in your footer component.

    – Dvid Silva
    Jan 2 at 17:35











  • thank you for your help @Dvid Silva ! OK so I have a component that wraps my entire app; Should I put this code in my ComponentDidMount() method ? If I want to use a separate index file, how can I do that ? create another entry point ?

    – Jules Corb
    Jan 3 at 8:26














  • 1





    That file is delivered as is. To add a pixel only in production you'd need to have a separate index file you use in the production build, or add that code into a component that has access to the env, like in your footer component.

    – Dvid Silva
    Jan 2 at 17:35











  • thank you for your help @Dvid Silva ! OK so I have a component that wraps my entire app; Should I put this code in my ComponentDidMount() method ? If I want to use a separate index file, how can I do that ? create another entry point ?

    – Jules Corb
    Jan 3 at 8:26








1




1





That file is delivered as is. To add a pixel only in production you'd need to have a separate index file you use in the production build, or add that code into a component that has access to the env, like in your footer component.

– Dvid Silva
Jan 2 at 17:35





That file is delivered as is. To add a pixel only in production you'd need to have a separate index file you use in the production build, or add that code into a component that has access to the env, like in your footer component.

– Dvid Silva
Jan 2 at 17:35













thank you for your help @Dvid Silva ! OK so I have a component that wraps my entire app; Should I put this code in my ComponentDidMount() method ? If I want to use a separate index file, how can I do that ? create another entry point ?

– Jules Corb
Jan 3 at 8:26





thank you for your help @Dvid Silva ! OK so I have a component that wraps my entire app; Should I put this code in my ComponentDidMount() method ? If I want to use a separate index file, how can I do that ? create another entry point ?

– Jules Corb
Jan 3 at 8:26












2 Answers
2






active

oldest

votes


















1














In most of my simple apps I choose to manage this by printing a JS variable into the html page. My prefer goto is Handlebars and I'll inject a config object into the header that will contain something like.



    <script type="application/javascript">
var CONFIG = {
fbPixel: '{{ fbPixel }}'
}
</script>


The fbPixel comes from a config file and is only populated in staging and production environments. In development it's undefined/empty. Quick and simple to setup. In my more complicated apps I still basically do the same thing, but use a helper function in the middle for validation and allowing of default options.






share|improve this answer
























  • Thank you for your help Russ Brown! that's interesting. I actually went with @Dvid Silva's solution that is creating to different entry points for dev and prod env. But This seems to be working as well !

    – Jules Corb
    Jan 3 at 8:31



















0














thanks to @Dvid Silva and @Russ Brown, I finaly went with the following solution: create two separate index.html files: dev-index.html and prod-index.html



in my config/path.js:



module.exports = {
#[...]
appHtml: process.env.NODE_ENV === 'production' ? resolveApp('public/prod-index.html') : resolveApp('public/dev-index.html'),
#[...]
};


Both index files are identical except the production one has the Facebook Pixel inside its <head> tag.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010016%2fset-up-facebook-pixel-only-for-production-in-reactjs%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    In most of my simple apps I choose to manage this by printing a JS variable into the html page. My prefer goto is Handlebars and I'll inject a config object into the header that will contain something like.



        <script type="application/javascript">
    var CONFIG = {
    fbPixel: '{{ fbPixel }}'
    }
    </script>


    The fbPixel comes from a config file and is only populated in staging and production environments. In development it's undefined/empty. Quick and simple to setup. In my more complicated apps I still basically do the same thing, but use a helper function in the middle for validation and allowing of default options.






    share|improve this answer
























    • Thank you for your help Russ Brown! that's interesting. I actually went with @Dvid Silva's solution that is creating to different entry points for dev and prod env. But This seems to be working as well !

      – Jules Corb
      Jan 3 at 8:31
















    1














    In most of my simple apps I choose to manage this by printing a JS variable into the html page. My prefer goto is Handlebars and I'll inject a config object into the header that will contain something like.



        <script type="application/javascript">
    var CONFIG = {
    fbPixel: '{{ fbPixel }}'
    }
    </script>


    The fbPixel comes from a config file and is only populated in staging and production environments. In development it's undefined/empty. Quick and simple to setup. In my more complicated apps I still basically do the same thing, but use a helper function in the middle for validation and allowing of default options.






    share|improve this answer
























    • Thank you for your help Russ Brown! that's interesting. I actually went with @Dvid Silva's solution that is creating to different entry points for dev and prod env. But This seems to be working as well !

      – Jules Corb
      Jan 3 at 8:31














    1












    1








    1







    In most of my simple apps I choose to manage this by printing a JS variable into the html page. My prefer goto is Handlebars and I'll inject a config object into the header that will contain something like.



        <script type="application/javascript">
    var CONFIG = {
    fbPixel: '{{ fbPixel }}'
    }
    </script>


    The fbPixel comes from a config file and is only populated in staging and production environments. In development it's undefined/empty. Quick and simple to setup. In my more complicated apps I still basically do the same thing, but use a helper function in the middle for validation and allowing of default options.






    share|improve this answer













    In most of my simple apps I choose to manage this by printing a JS variable into the html page. My prefer goto is Handlebars and I'll inject a config object into the header that will contain something like.



        <script type="application/javascript">
    var CONFIG = {
    fbPixel: '{{ fbPixel }}'
    }
    </script>


    The fbPixel comes from a config file and is only populated in staging and production environments. In development it's undefined/empty. Quick and simple to setup. In my more complicated apps I still basically do the same thing, but use a helper function in the middle for validation and allowing of default options.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 2 at 19:03









    Russ BrownRuss Brown

    415




    415













    • Thank you for your help Russ Brown! that's interesting. I actually went with @Dvid Silva's solution that is creating to different entry points for dev and prod env. But This seems to be working as well !

      – Jules Corb
      Jan 3 at 8:31



















    • Thank you for your help Russ Brown! that's interesting. I actually went with @Dvid Silva's solution that is creating to different entry points for dev and prod env. But This seems to be working as well !

      – Jules Corb
      Jan 3 at 8:31

















    Thank you for your help Russ Brown! that's interesting. I actually went with @Dvid Silva's solution that is creating to different entry points for dev and prod env. But This seems to be working as well !

    – Jules Corb
    Jan 3 at 8:31





    Thank you for your help Russ Brown! that's interesting. I actually went with @Dvid Silva's solution that is creating to different entry points for dev and prod env. But This seems to be working as well !

    – Jules Corb
    Jan 3 at 8:31













    0














    thanks to @Dvid Silva and @Russ Brown, I finaly went with the following solution: create two separate index.html files: dev-index.html and prod-index.html



    in my config/path.js:



    module.exports = {
    #[...]
    appHtml: process.env.NODE_ENV === 'production' ? resolveApp('public/prod-index.html') : resolveApp('public/dev-index.html'),
    #[...]
    };


    Both index files are identical except the production one has the Facebook Pixel inside its <head> tag.






    share|improve this answer




























      0














      thanks to @Dvid Silva and @Russ Brown, I finaly went with the following solution: create two separate index.html files: dev-index.html and prod-index.html



      in my config/path.js:



      module.exports = {
      #[...]
      appHtml: process.env.NODE_ENV === 'production' ? resolveApp('public/prod-index.html') : resolveApp('public/dev-index.html'),
      #[...]
      };


      Both index files are identical except the production one has the Facebook Pixel inside its <head> tag.






      share|improve this answer


























        0












        0








        0







        thanks to @Dvid Silva and @Russ Brown, I finaly went with the following solution: create two separate index.html files: dev-index.html and prod-index.html



        in my config/path.js:



        module.exports = {
        #[...]
        appHtml: process.env.NODE_ENV === 'production' ? resolveApp('public/prod-index.html') : resolveApp('public/dev-index.html'),
        #[...]
        };


        Both index files are identical except the production one has the Facebook Pixel inside its <head> tag.






        share|improve this answer













        thanks to @Dvid Silva and @Russ Brown, I finaly went with the following solution: create two separate index.html files: dev-index.html and prod-index.html



        in my config/path.js:



        module.exports = {
        #[...]
        appHtml: process.env.NODE_ENV === 'production' ? resolveApp('public/prod-index.html') : resolveApp('public/dev-index.html'),
        #[...]
        };


        Both index files are identical except the production one has the Facebook Pixel inside its <head> tag.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 3 at 10:12









        Jules CorbJules Corb

        494515




        494515






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010016%2fset-up-facebook-pixel-only-for-production-in-reactjs%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith