javascript - Loading Facebook pixel async - Stack Overflow

I'm loading the Facebook pixel and getting an error because the script isn't loaded when the

I'm loading the Facebook pixel and getting an error because the script isn't loaded when the call is made. This is what it looks like:

function Load3rdPartyScripts() {

    (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', '.js', undefined, undefined, undefined);

    fbq('init', 'XXXXXXXXXXXXX'); //problem here
    fbq('track', 'PageView');

}

Problem is that fbq isn't initiated when the code runs. Now I know I could wrap the problem code inside a setInterval and keep checking until the external script is fully loaded but I'm wondering if there's a better way to do it.

I'm loading the Facebook pixel and getting an error because the script isn't loaded when the call is made. This is what it looks like:

function Load3rdPartyScripts() {

    (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/en_US/fbevents.js', undefined, undefined, undefined);

    fbq('init', 'XXXXXXXXXXXXX'); //problem here
    fbq('track', 'PageView');

}

Problem is that fbq isn't initiated when the code runs. Now I know I could wrap the problem code inside a setInterval and keep checking until the external script is fully loaded but I'm wondering if there's a better way to do it.

Share Improve this question edited May 8, 2018 at 11:02 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Jul 2, 2017 at 17:11 frenchiefrenchie 52.1k117 gold badges320 silver badges528 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4 +50

I tried to reproduce this problem but in vain. Your code works perfectly. fbq is appended syncronously (So no need to timeout) to window and will be called normally. Check yourself:

(function Load3rdPartyScripts() {

    (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/en_US/fbevents.js', undefined, undefined, undefined);

    fbq('init', 'XXXXXXXXXXXXX'); // No problem here!
    fbq('track', 'PageView');

})();

But

There are some potential reasons that may produce your error:

  1. Running the script outside the browser, An env that ruined the window variable.

  2. Running code on browser but with a freezed window (Object.freeze(window)) causes that fbq will be undefined

  3. So also Object.seal(window) like freeze(). It will prevent new properties from being added to window

  4. Creating a local scope named window variable will cause fbq to be undefined

    function Load3rdPartyScripts() {
        let window = {};  // Notice!
        (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/en_US/fbevents.js', undefined, undefined, undefined);
    
        fbq('init', 'XXXXXXXXXXXXX'); //problem here
        fbq('track', 'PageView');
    
    }
    Load3rdPartyScripts();
    

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745140907a4613426.html

相关推荐

  • javascript - Loading Facebook pixel async - Stack Overflow

    I'm loading the Facebook pixel and getting an error because the script isn't loaded when the

    6小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信