* Segment serverside analytics plugin * @link https://getanalytics.io/plugins/intercom/ * @link https://github.com/intercom/intercom-node/ * @param {object} pluginConfig - Plugin settings * @param {string} pluginConfig.appId - Your Intercom app id * @return {object} Analytics plugin * @examp
(userConfig = {})
| 24 | * }) |
| 25 | */ |
| 26 | function intercomPlugin(userConfig = {}) { |
| 27 | const config = { |
| 28 | ...defaultConfig, |
| 29 | ...userConfig, |
| 30 | } |
| 31 | |
| 32 | const client = new Intercom.Client({ token: config.appId }) |
| 33 | |
| 34 | return { |
| 35 | name: 'intercom', |
| 36 | config: config, |
| 37 | // Custom intercom methods |
| 38 | methods: { |
| 39 | // Function for using intercom-node client in other methods |
| 40 | getClient: () => client, |
| 41 | }, |
| 42 | /* page view */ |
| 43 | page: ({ payload, config }) => { |
| 44 | console.log('page event not yet implemented, doesn\'t seem to be available from node sdk') |
| 45 | }, |
| 46 | /* track event */ |
| 47 | track: ({ payload, config }) => { |
| 48 | const { userId } = payload |
| 49 | if (!userId && !anonymousId) { |
| 50 | throw new Error('Missing userId. You must include one to make intercom call') |
| 51 | } |
| 52 | const data = { |
| 53 | event_name: payload.event, |
| 54 | created_at: Math.floor(new Date().getTime() / 1000), |
| 55 | user_id: userId, |
| 56 | metadata: payload.properties, |
| 57 | } |
| 58 | client.events.create(data) |
| 59 | }, |
| 60 | /* identify user */ |
| 61 | identify: ({ payload }) => { |
| 62 | const { userId, traits } = payload |
| 63 | |
| 64 | const email = isEmail(userId) ? userId : traits.email |
| 65 | if (!email) { |
| 66 | throw new Error('Missing email. userId or traits.email must be set') |
| 67 | } |
| 68 | |
| 69 | client.users.create({ |
| 70 | email: email, |
| 71 | custom_attributes: traits |
| 72 | }) |
| 73 | }, |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | export default intercomPlugin |