Skip to content

Commit

Permalink
Merge pull request #26 from ikbenben/features/intercom-settings
Browse files Browse the repository at this point in the history
feat: adding config object for messenger attributes
  • Loading branch information
Jamiewarb authored Jun 14, 2021
2 parents bfae910 + f1e7b79 commit 49e7a66
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 4 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ const defaults = {
appId: null, // Intercom ID
autoBoot: true, // True to boot messenger widget and show UI on page load, false to allow manually booting later
debug: false, // True to show debug messages in the console, useful for development, false to not show them


config: {}, // Object to specify messenger attributes to configure when booting. see https://developers.intercom.com/installing-intercom/docs/javascript-api-attributes-objects#section-messenger-attributes

scriptId: 'intercom-script', // String to identfy the script tag, for vue-meta
scriptDefer: false, // True to defer loading intercom widget javascript until page loads, false to async load it in document flow

updateOnPageRoute: true // True to call intercom's 'update' method on route change, false to not do this
};
```
Expand Down
2 changes: 1 addition & 1 deletion example/nuxt.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { resolve } = require('path')

module.exports = {
mode: 'spa',
ssr: true,
rootDir: resolve(__dirname, '..'),
buildDir: resolve(__dirname, '.nuxt'),
head: {
Expand Down
7 changes: 7 additions & 0 deletions lib/Intercom.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export default class Intercom {
appId;
debug;
config;
ready;
unreadCount;
userData;
Expand All @@ -10,6 +11,7 @@ export default class Intercom {
constructor (appId, settings = {}, { userData = {} } = {}) {
this.appId = appId
this.debug = settings.debug || false
this.config = settings.config || {}
this.ready = false
this.unreadCount = 0
this.userData = userData
Expand Down Expand Up @@ -57,6 +59,11 @@ export default class Intercom {
if (!window.intercomSettings) { window.intercomSettings = {} }
window.intercomSettings.app_id = this.appId

Object.keys(this.config).forEach((key) => {
const snakeCaseKey = key.replace(/([A-Z])/g, '_$1').toLowerCase()
window.intercomSettings[snakeCaseKey] = this.config[key]
})

return this._updateData('boot', userData)
}

Expand Down
2 changes: 2 additions & 0 deletions lib/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const defaults = {
autoBoot: true, // True to boot messenger widget and show UI on page load, false to allow manually booting later
debug: false, // True to show debug messages in the console, useful for development, false to not show them

config: {}, // Object to specify messenger attributes to configure when booting. see https://developers.intercom.com/installing-intercom/docs/javascript-api-attributes-objects#section-messenger-attributes

scriptId: 'intercom-script', // String to identfy the script tag, for vue-meta
scriptDefer: false, // True to defer loading intercom widget javascript until page loads, false to async load it in document flow

Expand Down
3 changes: 2 additions & 1 deletion lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ const appId = '<%= options.appId %>';
const autoBoot = <%= options.autoBoot || false %>;
const debug = <%= options.debug || false %>;
const pageTracking = <%= options.updateOnPageRoute || false %>;
const config = <%= options.config ? JSON.stringify(options.config) : '{}' %>;
let $intercom = null;
let intercomInstalled = false;

export default function (ctx, inject) {
$intercom = new Intercom(appId, { debug });
$intercom = new Intercom(appId, { debug, config });
inject('intercom', $intercom);

const intercomAppMixin = {
Expand Down
10 changes: 10 additions & 0 deletions test/Intercom.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ describe('Intercom.js', () => {
)
})

test('can boot with messenger attributes', () => {
const config = { customLauncherSelector: '.intercom-btn', hideDefaultLauncher: true }

intercom = new Intercom(appId, { config })
intercom.boot()

expect(global.window.intercomSettings.custom_launcher_selector).toEqual('.intercom-btn')
expect(global.window.intercomSettings.hide_default_launcher).toEqual(true)
})

test('can change appId on boot', () => {
const newAppId = 'newId123'
intercom.boot({ app_id: newAppId })
Expand Down

0 comments on commit 49e7a66

Please sign in to comment.