|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Module dependencies. |
| 5 | + */ |
| 6 | + |
| 7 | +var https = require('use-https'); |
| 8 | +var integration = require('@segment/analytics.js-integration'); |
| 9 | +var tick = require('next-tick'); |
| 10 | + |
| 11 | +/** |
| 12 | + * Expose `Olark` integration. |
| 13 | + */ |
| 14 | + |
| 15 | +var Olark = module.exports = integration('Olark') |
| 16 | + .assumesPageview() |
| 17 | + .global('olark') |
| 18 | + .option('groupId', '') |
| 19 | + .option('identify', true) |
| 20 | + .option('listen', false) |
| 21 | + .option('page', true) |
| 22 | + .option('siteId', '') |
| 23 | + .option('track', false) |
| 24 | + .option('inline', false); |
| 25 | + |
| 26 | +/** |
| 27 | + * The context for this integration. |
| 28 | + */ |
| 29 | + |
| 30 | +var integrationContext = { |
| 31 | + name: 'olark', |
| 32 | + version: '1.0.0' |
| 33 | +}; |
| 34 | + |
| 35 | +/** |
| 36 | + * Initialize. |
| 37 | + * |
| 38 | + * http://www.olark.com/documentation |
| 39 | + * https://www.olark.com/documentation/javascript/api.chat.setOperatorGroup |
| 40 | + * |
| 41 | + * @api public |
| 42 | + */ |
| 43 | + |
| 44 | +Olark.prototype.initialize = function() { |
| 45 | + var self = this; |
| 46 | + this.load(function() { |
| 47 | + tick(self.ready); |
| 48 | + }); |
| 49 | + |
| 50 | + // assign chat to a specific site |
| 51 | + var groupId = this.options.groupId; |
| 52 | + if (groupId) api('chat.setOperatorGroup', { group: groupId }); |
| 53 | + |
| 54 | + // keep track of the widget's open state |
| 55 | + api('box.onExpand', function() { self._open = true; }); |
| 56 | + api('box.onShrink', function() { self._open = false; }); |
| 57 | + |
| 58 | + // record events |
| 59 | + if (this.options.listen) this.attachListeners(); |
| 60 | +}; |
| 61 | + |
| 62 | +/** |
| 63 | + * Load. |
| 64 | + * |
| 65 | + * @api private |
| 66 | + * @param {Function} callback |
| 67 | + */ |
| 68 | + |
| 69 | +Olark.prototype.load = function(callback) { |
| 70 | + /* eslint-disable */ |
| 71 | + window.olark||(function(c){var f=window,d=document,l=https()?"https:":"http:",z=c.name,r="load";var nt=function(){f[z]=function(){(a.s=a.s||[]).push(arguments)};var a=f[z]._={},q=c.methods.length;while (q--) {(function(n){f[z][n]=function(){f[z]("call",n,arguments)}})(c.methods[q])}a.l=c.loader;a.i=nt;a.p={ 0:+new Date() };a.P=function(u){a.p[u]=new Date()-a.p[0]};function s(){a.P(r);f[z](r)}f.addEventListener?f.addEventListener(r,s,false):f.attachEvent("on"+r,s);var ld=function(){function p(hd){hd="head";return ["<",hd,"></",hd,"><",i,' onl' + 'oad="var d=',g,";d.getElementsByTagName('head')[0].",j,"(d.",h,"('script')).",k,"='",l,"//",a.l,"'",'"',"></",i,">"].join("")}var i="body",m=d[i];if (!m) {return setTimeout(ld,100)}a.P(1);var j="appendChild",h="createElement",k="src",n=d[h]("div"),v=n[j](d[h](z)),b=d[h]("iframe"),g="document",e="domain",o;n.style.display="none";m.insertBefore(n,m.firstChild).id=z;b.frameBorder="0";b.id=z+"-loader";if (/MSIE[ ]+6/.test(navigator.userAgent)) {b.src="javascript:false"}b.allowTransparency="true";v[j](b);try {b.contentWindow[g].open()}catch (w) {c[e]=d[e];o="javascript:var d="+g+".open();d.domain='"+d.domain+"';";b[k]=o+"void(0);"}try {var t=b.contentWindow[g];t.write(p());t.close()}catch (x) {b[k]=o+'d.write("'+p().replace(/"/g,String.fromCharCode(92)+'"')+'");d.close();'}a.P(2)};ld()};nt()})({ loader: "static.olark.com/jsclient/loader0.js", name:"olark", methods:["configure","extend","declare","identify"] }); |
| 72 | + /* eslint-enable */ |
| 73 | + |
| 74 | + // check if chat should be loaded as `inline chat` |
| 75 | + if (this.options.inline) configure('box.inline', true); |
| 76 | + |
| 77 | + window.olark.identify(this.options.siteId); |
| 78 | + callback(); |
| 79 | +}; |
| 80 | + |
| 81 | +/** |
| 82 | + * Loaded? |
| 83 | + * |
| 84 | + * @api private |
| 85 | + * @return {boolean} |
| 86 | + */ |
| 87 | + |
| 88 | +Olark.prototype.loaded = function() { |
| 89 | + return !!window.olark; |
| 90 | +}; |
| 91 | + |
| 92 | +/** |
| 93 | + * Page. |
| 94 | + * |
| 95 | + * @param {Facade} page |
| 96 | + */ |
| 97 | + |
| 98 | +Olark.prototype.page = function(page) { |
| 99 | + if (!this.options.page) return; |
| 100 | + var props = page.properties(); |
| 101 | + var name = page.fullName(); |
| 102 | + if (!name && !props.url) return; |
| 103 | + |
| 104 | + name = name ? name + ' page' : props.url; |
| 105 | + this.notify('looking at ' + name); |
| 106 | +}; |
| 107 | + |
| 108 | +/** |
| 109 | + * Identify. |
| 110 | + * |
| 111 | + * @param {Facade} identify |
| 112 | + */ |
| 113 | + |
| 114 | +Olark.prototype.identify = function(identify) { |
| 115 | + if (!this.options.identify) return; |
| 116 | + |
| 117 | + var username = identify.username(); |
| 118 | + var traits = identify.traits(); |
| 119 | + var id = identify.userId(); |
| 120 | + var email = identify.email(); |
| 121 | + var phone = identify.phone(); |
| 122 | + var name = identify.name() || identify.firstName(); |
| 123 | + |
| 124 | + if (traits) api('visitor.updateCustomFields', traits); |
| 125 | + if (email) api('visitor.updateEmailAddress', { emailAddress: email }); |
| 126 | + if (phone) api('visitor.updatePhoneNumber', { phoneNumber: phone }); |
| 127 | + if (name) api('visitor.updateFullName', { fullName: name }); |
| 128 | + |
| 129 | + // figure out best nickname |
| 130 | + var nickname = name || email || username || id; |
| 131 | + if (name && email) nickname += ' (' + email + ')'; |
| 132 | + if (nickname) api('chat.updateVisitorNickname', { snippet: nickname }); |
| 133 | +}; |
| 134 | + |
| 135 | +/** |
| 136 | + * Track. |
| 137 | + * |
| 138 | + * @api public |
| 139 | + * @param {Facade} track |
| 140 | + */ |
| 141 | + |
| 142 | +Olark.prototype.track = function(track) { |
| 143 | + if (!this.options.track) return; |
| 144 | + this.notify('visitor triggered "' + track.event() + '"'); |
| 145 | +}; |
| 146 | + |
| 147 | +/** |
| 148 | + * Listen for events. |
| 149 | + */ |
| 150 | + |
| 151 | +Olark.prototype.attachListeners = function() { |
| 152 | + var self = this; |
| 153 | + |
| 154 | + api('chat.onBeginConversation', function() { |
| 155 | + self.analytics.track( |
| 156 | + 'Live Chat Conversation Started', |
| 157 | + {}, |
| 158 | + { |
| 159 | + context: { integration: integrationContext }, |
| 160 | + integrations: { Olark: false } |
| 161 | + } |
| 162 | + ); |
| 163 | + }); |
| 164 | + |
| 165 | + // Callback accepts `event` |
| 166 | + // TODO: We might eventually send information about the event through Segment |
| 167 | + api('chat.onMessageToOperator', function() { |
| 168 | + self.analytics.track( |
| 169 | + 'Live Chat Message Sent', |
| 170 | + {}, |
| 171 | + { |
| 172 | + context: { integration: integrationContext }, |
| 173 | + integrations: { Olark: false } |
| 174 | + } |
| 175 | + ); |
| 176 | + }); |
| 177 | + |
| 178 | + // Callback accepts `event` |
| 179 | + // TODO: We might eventually send information about the event through Segment |
| 180 | + api('chat.onMessageToVisitor', function() { |
| 181 | + self.analytics.track( |
| 182 | + 'Live Chat Message Received', |
| 183 | + {}, |
| 184 | + { |
| 185 | + context: { integration: integrationContext }, |
| 186 | + integrations: { Olark: false } |
| 187 | + } |
| 188 | + ); |
| 189 | + }); |
| 190 | +}; |
| 191 | + |
| 192 | +/** |
| 193 | + * Send a notification `message` to the operator, only when a chat is active and |
| 194 | + * when the chat is open. |
| 195 | + * |
| 196 | + * @api private |
| 197 | + * @param {string} message |
| 198 | + */ |
| 199 | + |
| 200 | +Olark.prototype.notify = function(message) { |
| 201 | + if (!this._open) return; |
| 202 | + |
| 203 | + // lowercase since olark does |
| 204 | + message = message.toLowerCase(); |
| 205 | + |
| 206 | + api('visitor.getDetails', function(data) { |
| 207 | + if (!data || !data.isConversing) return; |
| 208 | + api('chat.sendNotificationToOperator', { body: message }); |
| 209 | + }); |
| 210 | +}; |
| 211 | + |
| 212 | +/** |
| 213 | + * Helper for Olark API calls. |
| 214 | + * |
| 215 | + * @api private |
| 216 | + * @param {string} action |
| 217 | + * @param {Object} value |
| 218 | + */ |
| 219 | + |
| 220 | +function api(action, value) { |
| 221 | + window.olark('api.' + action, value); |
| 222 | +} |
| 223 | + |
| 224 | +function configure(action, value) { |
| 225 | + window.olark.configure(action, value); |
| 226 | +} |
0 commit comments