Intercom + Discourse

This is in part based on @jesselperry’s previous comment, but updated with the latest Intercom messenger and implemented according to Intercom’s setup guide for single page apps. The following is what I have and it seems to be working ok.

First add this to the </head> section:

<script>
(function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic('reattach_activator');ic('update',intercomSettings);}else{var d=document;var i=function(){i.c(arguments)};i.q=[];i.c=function(args){i.q.push(args)};w.Intercom=i;function l(){var s=d.createElement('script');s.type='text/javascript';s.async=true;
s.src='https://widget.intercom.io/widget/[YOUR_APP_ID]';
var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s,x);}if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}}})()
</script>

And add this to the </body> section:

<script>
$.getJSON("https://[YOUR_DISCOURSE_DOMAIN]/session/current.json", function(json){
    var currentusernamefromjson = String(json.current_user.username);
    var currentuserflname = String(json.current_user.name);
    var fulllinktojson = "https://[YOUR_DISCOURSE_DOMAIN]/users/" + currentusernamefromjson + ".json";
    $.getJSON(fulllinktojson, function(json){
        var currentuseremail = String(json.user.email);
        var currentuserid = String(json.user.id);

        window.Intercom('boot', {
            app_id: '[YOUR_APP_ID]',
            email: currentuseremail,
            name: currentuserflname,
            "discourse_username": currentusernamefromjson,
            "discourse_uid": currentuserid
        });
    });
});
</script>

Make sure to replace [YOUR_APP_ID] with your Intercom app ID, and [YOUR_DISCOURSE_DOMAIN] with your domain (and change https to http if that’s what you use).

5 Likes