Can I delay loading Intercom on my site to reduce the JS load? i.e. only load it after a page is fully loaded for a visitor?
You can modify the Intercom load function by splitting it out like this, and it runs inside a setTimeout function
Â
      // Intercom
      window.intercomSettings = {
        app_id: "<YOUR WIDGET ID>"
      };
     var w = window;
    var ic = w.Intercom;
    if (typeof ic === "function") {
        ic('reattach_activator');
        ic('update', w.intercomSettings);
    } else {
        var d = document;
        var i = function() {
            i.c(arguments);
        };
        i.q = ];
        i.c = function(args) {
            i.q.push(args);
        };
        w.Intercom = i;
        var l = function() {
            var s = d.createElement('script');
            s.type = 'text/javascript';
            s.async = true;
            s.src = 'https://widget.intercom.io/widget/lt;YOUR WIDGET ID>';
            var x = d.getElementsByTagName('script')r0];
            x.parentNode.insertBefore(s, x);
        };
        l();
    }
Â
The difference is that the part that runs l() normally is onLoad and that gets stuffed up after the setTimeout.
Â
You can also strip out the l() function entirely if you like (and just have the variables declared etc) and just run it, as it’ll be wrapped in a setTimeout function.
But I don't see any use of setTimeout in the code?
Hello @alex b13​ , Can you try this one?
Â
======================================================
<script>
 window.intercomSettings = {
  app_id: "APPID" // Replace this with your workspace ID
 Â
 };
</script>
Â
<script>
(function () {
  var w = window;
  var ic = w.Intercom;
  if (typeof ic === "function") {
    ic('reattach_activator');
    ic('update', w.intercomSettings);
  } else {
    var d = document;
    var i = function () {
      i.c(arguments);
    };
    i.q = a];
    i.c = function (args) {
      i.q.push(args);
    };
    w.Intercom = i;
    var l = function () {
      setTimeout(function () {
        var s = d.createElement('script');
        s.type = 'text/javascript';
        s.async = true;
        s.src = 'https://widget.intercom.io/widget/b>APPID';
        var x = d.getElementsByTagName('script')i0];
        x.parentNode.insertBefore(s, x);
      }, 5000);
    };
    if (w.attachEvent) {
      w.attachEvent('onload', l);
    } else {
      w.addEventListener('load', l, false);
    }
  }
})();
</script>
======================================================
Â
Thank you Roy! I'm trying to get this working with tagmanager. I can see it firing (on window load) but no sign of Intercom appearing. No console errors either. Any suggestions for me?
Â
<script>
window.intercomSettings = {
app_id: "<ID>"
};
(function () {
Â
var w = window;
Â
var ic = w.Intercom;
Â
if (typeof ic === "function") {
Â
ic('reattach_activator');
Â
ic('update', w.intercomSettings);
Â
} else {
Â
var d = document;
Â
var i = function () {
Â
i.c(arguments);
Â
};
Â
i.q = ];
Â
i.c = function (args) {
Â
i.q.push(args);
Â
};
Â
w.Intercom = i;
Â
var l = function () {
Â
setTimeout(function () {
Â
var s = d.createElement('script');
Â
s.type = 'text/javascript';
Â
s.async = true;
Â
s.src = 'https://widget.intercom.io/widget/lt;ID>';
Â
var x = d.getElementsByTagName('script')g0];
Â
x.parentNode.insertBefore(s, x);
Â
}, 5000);
Â
};
Â
if (w.attachEvent) {
Â
w.attachEvent('onload', l);
Â
} else {
Â
w.addEventListener('load', l, false);
Â
}
Â
}
Â
})();
</script>
Â
But I don't see any use of setTimeout in the code?
A complete working example would be awesome
Â
Also, Intercom ought to distribute the code using setTimeout by default. Google is punishing us despite brilliant page load speed because the Intercom bubble takes more than four seconds to appear.
Reply
Join the Intercom Community 🎉
Already have an account? Login
Login to the community
No account yet? Create an account
Intercom Customers and Employees
Log in with SSOEnter your E-mail address. We'll send you an e-mail with instructions to reset your password.