1+
2+ 'use strict' ;
3+
4+ /**
5+ * Module dependencies.
6+ */
7+
8+ var integration = require ( '@segment/analytics.js-integration' ) ;
9+ var uncase = require ( 'to-no-case' ) ;
10+ var foldl = require ( '@ndhoule/foldl' ) ;
11+ var Identify = require ( 'segmentio-facade' ) . Identify ;
12+
13+ /**
14+ * Expose `Outbound` integration.
15+ */
16+
17+ var Outbound = module . exports = integration ( 'Outbound' )
18+ . global ( 'outbound' )
19+ . option ( 'publicApiKey' , '' )
20+ . option ( 'trackReferrer' , false )
21+ . tag ( '<script src="//cdn.outbound.io/{{ publicApiKey }}.js">' ) ;
22+
23+ /**
24+ * Initialize.
25+ *
26+ * @api public
27+ */
28+
29+ Outbound . prototype . initialize = function ( ) {
30+ window . outbound = window . outbound || [ ] ;
31+ window . outbound . methods = [
32+ 'identify' ,
33+ 'track' ,
34+ 'alias' ,
35+ 'registerApnsToken' ,
36+ 'registerGcmToken' ,
37+ 'disableApnsToken' ,
38+ 'disableGcmToken' ,
39+ 'disableAllGcmTokens' ,
40+ 'disableAllApnsTokens' ,
41+ 'unsubscribeAll' ,
42+ 'unsubscribeCampaigns' ,
43+ 'subscribeAll' ,
44+ 'subscribeCampaigns' ,
45+ 'hasIdentified'
46+ ] ;
47+
48+ window . outbound . factory = function ( method ) {
49+ return function ( ) {
50+ var args = Array . prototype . slice . call ( arguments ) ;
51+ args . unshift ( method ) ;
52+ window . outbound . push ( args ) ;
53+ return window . outbound ;
54+ } ;
55+ } ;
56+
57+ for ( var i = 0 ; i < window . outbound . methods . length ; i ++ ) {
58+ var key = window . outbound . methods [ i ] ;
59+ window . outbound [ key ] = window . outbound . factory ( key ) ;
60+ }
61+
62+ this . load ( this . ready ) ;
63+ } ;
64+
65+ /**
66+ * Loaded
67+ *
68+ * @api private
69+ * @return {boolean }
70+ */
71+
72+ Outbound . prototype . loaded = function ( ) {
73+ return ! ! ( window . outbound && window . outbound . reset ) ;
74+ } ;
75+
76+ /**
77+ * Identify.
78+ *
79+ * @api public
80+ * @param {Identify } identify
81+ */
82+
83+ Outbound . prototype . identify = function ( identify ) {
84+ var specialTraits = {
85+ id : true ,
86+ email : true ,
87+ phone : true ,
88+ 'user id' : true ,
89+ 'last name' : true ,
90+ 'first name' : true
91+ } ;
92+
93+ var userId = identify . userId ( ) || identify . anonymousId ( ) ;
94+
95+ var attributes = foldl ( function ( acc , val , key ) {
96+ if ( ! specialTraits . hasOwnProperty ( uncase ( key ) ) ) acc . attributes [ key ] = val ;
97+ return acc ;
98+ } , {
99+ attributes : { } ,
100+ email : identify . email ( ) ,
101+ phoneNumber : identify . phone ( ) ,
102+ firstName : identify . firstName ( ) ,
103+ lastName : identify . lastName ( )
104+ } , identify . traits ( ) ) ;
105+
106+ window . outbound . identify ( userId , attributes ) ;
107+ } ;
108+
109+ /**
110+ * Track.
111+ *
112+ * @api public
113+ * @param {Track } track
114+ */
115+
116+ Outbound . prototype . track = function ( track ) {
117+ if ( ! window . outbound . hasIdentified ( ) ) {
118+ var user = new Identify ( { userId : track . userId ( ) || track . anonymousId ( ) } ) ;
119+ this . identify ( user ) ;
120+ }
121+ window . outbound . track ( track . event ( ) , track . properties ( ) , track . timestamp ( ) ) ;
122+ } ;
123+
124+ /**
125+ * Alias.
126+ *
127+ * @api public
128+ * @param {Alias } alias
129+ */
130+
131+ Outbound . prototype . alias = function ( alias ) {
132+ window . outbound . identify ( alias . userId ( ) , { previousId : alias . previousId ( ) } ) ;
133+ } ;
134+
135+ /**
136+ * Page.
137+ *
138+ * @api public
139+ * @param {Page } page
140+ */
141+ Outbound . prototype . page = function ( page ) {
142+ var props = page . properties ( ) ;
143+ var evtName = '[Segment Page]' ;
144+
145+ if ( ! this . options . trackReferrer ) {
146+ delete props . referrer ;
147+ }
148+
149+ if ( props . name || props . url ) {
150+ evtName += ' ' + props . name || props . url ;
151+ }
152+
153+ if ( ! window . outbound . hasIdentified ( ) ) {
154+ var user = new Identify ( { userId : page . userId ( ) || page . anonymousId ( ) } ) ;
155+ this . identify ( user ) ;
156+ }
157+
158+ window . outbound . track ( evtName , props , page . timestamp ( ) ) ;
159+ } ;
0 commit comments