intercomメッセージ向けトラッキングURLをhubot上に構築する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Intercomのメッセージ内向けのトラッキング用エンドポイントを用意する。 | |
# 以下の機能がある。 | |
# | |
# - クリックしたら指定したカスタム属性が指定した値に更新される | |
# - 指定したURLにリダイレクトする | |
# - クリックしたことをSlackに通知する | |
# - Slack上でURLを確認できる | |
# - 複数指定可能 | |
# | |
# 実際にトラッキングに仕込むURLをslack上で確認する時は、 @hubot links で確認できる | |
# | |
# # Requirements | |
# | |
# 以下を実行してintercom-clientパッケージを取得 | |
# npm install --save intercom-client | |
# | |
# hubotの実行環境では環境変数 INTERCOM_API_TOKEN が必要 | |
# これはintercom developer hubで確認できる | |
# | |
# # Configurations | |
# | |
# HUBOT(express)サーバのドメイン | |
HUBOT_BASE = 'https://yourhubotdomain.herokuapp.com' | |
# | |
# クリック通知するSlackチャンネル | |
MENTION_ROOM = '#tracking-notification' | |
# | |
# トラッキングリンクの一覧(JSON) | |
TrackingLinks = require('./tracking_links.json') | |
# | |
Intercom = require('intercom-client') | |
UUID_REGEXP = /[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}/i | |
client = new Intercom.Client({ token: process.env.INTERCOM_API_TOKEN }) | |
updateIntercomData = (type, user_id, link) -> | |
new Promise (resolve, reject) -> | |
(if type is 'lead' then client.leads else client.users).find { user_id: user_id }, (res) -> | |
unless res.statusCode is 200 | |
reject("Lead or user not found: #{user_id}") | |
return | |
user = res.body | |
attrs = {} | |
attrs[link.attribute_name] = link.attribute_value | |
(if type is 'lead' then client.leads else client.users).update { id: user.id, custom_attributes: attrs }, (res) -> | |
unless res.statusCode is 200 | |
reject("Could not update lead or user: #{user_id}") | |
return | |
resolve(user) | |
module.exports = (robot) -> | |
robot.router.get "/links/:name", (req, res) -> | |
name = req.param('name') | |
link = TrackingLinks[name] | |
unless link | |
res.send(404, "#{name}というリンクは存在しません") | |
return | |
user_id = req.param('user_id') | |
type = if "#{user_id}".match(UUID_REGEXP) then 'lead' else 'user' | |
if user_id | |
setTimeout(() -> | |
updateIntercomData(type, user_id, link) | |
.then (user) -> | |
msg = """ | |
#{link.name} がクリックされました | |
type: #{type} | |
user.name: #{user.name} | |
user.email: #{user.email} | |
""" | |
robot.messageRoom MENTION_ROOM, msg | |
.catch (e) -> | |
console.error(e) | |
, 0) | |
res.redirect(link.to) | |
res.end() | |
robot.respond /links/i, (res)-> | |
linkmsgs = [] | |
Object.keys(TrackingLinks).forEach (key) -> | |
link = TrackingLinks[key] | |
linkmsgs.push """ | |
#{link.name} | |
URL: #{link.to} | |
Intercom 属性名: #{link.attribute_name} | |
Intercom 属性値: #{link.attribute_value} | |
トラッキングURL: `#{HUBOT_BASE}/links/#{key}?user_id={{ user_id | fallback:"_REPLACE_THIS_" }}` | |
""" | |
res.reply """ | |
トラッキングリンク一覧(#{Object.keys(TrackingLinks).length}) | |
-------------------- | |
#{linkmsgs.join("--------------------\n")} | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"resource": { | |
"name": "資料ダウンロード", | |
"to": "https://url-which-clicked-user-is-redirected-to", | |
"attribute_name": "resource_link_clicked", | |
"attribute_value": true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment