1010 * OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions
1111 * and limitations under the License.
1212 */
13- import { setUpBrowserPerformance } from " ./browser/BrowserUtil" ;
14- import { ClickstreamAnalytics , Item , SendMode } from '../src' ;
13+ import { setUpBrowserPerformance } from ' ./browser/BrowserUtil' ;
14+ import { ClickstreamAnalytics , Item , SendMode , Attr } from '../src' ;
1515import { NetRequest } from '../src/network/NetRequest' ;
1616import { Event } from '../src/provider' ;
1717import { StorageUtil } from '../src/util/StorageUtil' ;
@@ -66,17 +66,72 @@ describe('ClickstreamAnalytics test', () => {
6666 ) ;
6767 const firstEvent = eventList [ 0 ] ;
6868 expect ( firstEvent . event_type ) . toBe ( Event . PresetEvent . FIRST_OPEN ) ;
69- expect ( firstEvent . user [ Event . ReservedAttribute . USER_FIRST_TOUCH_TIMESTAMP ] ) . not . toBeUndefined ( )
69+ expect (
70+ firstEvent . user [ Event . ReservedAttribute . USER_FIRST_TOUCH_TIMESTAMP ]
71+ ) . not . toBeUndefined ( ) ;
7072 expect ( firstEvent . attributes . brand ) . toBe ( 'Samsung' ) ;
7173 expect ( firstEvent . attributes . level ) . toBe ( 10 ) ;
72- expect ( firstEvent . attributes [ Event . ReservedAttribute . SESSION_ID ] ) . not . toBeUndefined ( ) ;
73- expect ( firstEvent . attributes [ Event . ReservedAttribute . SESSION_NUMBER ] ) . not . toBeUndefined ( ) ;
74- expect ( firstEvent . attributes [ Event . ReservedAttribute . SESSION_START_TIMESTAMP ] ) . not . toBeUndefined ( ) ;
75- expect ( firstEvent . attributes [ Event . ReservedAttribute . SESSION_DURATION ] ) . not . toBeUndefined ( ) ;
74+ expect (
75+ firstEvent . attributes [ Event . ReservedAttribute . SESSION_ID ]
76+ ) . not . toBeUndefined ( ) ;
77+ expect (
78+ firstEvent . attributes [ Event . ReservedAttribute . SESSION_NUMBER ]
79+ ) . not . toBeUndefined ( ) ;
80+ expect (
81+ firstEvent . attributes [ Event . ReservedAttribute . SESSION_START_TIMESTAMP ]
82+ ) . not . toBeUndefined ( ) ;
83+ expect (
84+ firstEvent . attributes [ Event . ReservedAttribute . SESSION_DURATION ]
85+ ) . not . toBeUndefined ( ) ;
7686 const testEvent = eventList [ eventList . length - 1 ] ;
7787 expect ( testEvent . attributes . brand ) . toBeUndefined ( ) ;
7888 } ) ;
7989
90+ test ( 'test init sdk with traffic source global attributes' , async ( ) => {
91+ const result = ClickstreamAnalytics . init ( {
92+ appId : 'testApp' ,
93+ endpoint : 'https://example.com/collect' ,
94+ sendMode : SendMode . Batch ,
95+ globalAttributes : {
96+ [ Attr . TRAFFIC_SOURCE_SOURCE ] : 'amazon' ,
97+ [ Attr . TRAFFIC_SOURCE_MEDIUM ] : 'cpc' ,
98+ [ Attr . TRAFFIC_SOURCE_CAMPAIGN ] : 'summer_promotion' ,
99+ [ Attr . TRAFFIC_SOURCE_CAMPAIGN_ID ] : 'summer_promotion_01' ,
100+ [ Attr . TRAFFIC_SOURCE_TERM ] : 'running_shoes' ,
101+ [ Attr . TRAFFIC_SOURCE_CONTENT ] : 'banner_ad_1' ,
102+ [ Attr . TRAFFIC_SOURCE_CLID ] : 'amazon_ad_123' ,
103+ [ Attr . TRAFFIC_SOURCE_CLID_PLATFORM ] : 'amazon_ads' ,
104+ } ,
105+ } ) ;
106+ expect ( result ) . toBeTruthy ( ) ;
107+ await sleep ( 100 ) ;
108+ const eventList = JSON . parse (
109+ StorageUtil . getAllEvents ( ) + Event . Constants . SUFFIX
110+ ) ;
111+ const firstEvent = eventList [ 0 ] ;
112+ expect ( firstEvent . event_type ) . toBe ( Event . PresetEvent . FIRST_OPEN ) ;
113+ expect ( firstEvent . attributes [ Attr . TRAFFIC_SOURCE_SOURCE ] ) . toBe ( 'amazon' ) ;
114+ expect ( firstEvent . attributes [ Attr . TRAFFIC_SOURCE_MEDIUM ] ) . toBe ( 'cpc' ) ;
115+ expect ( firstEvent . attributes [ Attr . TRAFFIC_SOURCE_CAMPAIGN ] ) . toBe (
116+ 'summer_promotion'
117+ ) ;
118+ expect ( firstEvent . attributes [ Attr . TRAFFIC_SOURCE_CAMPAIGN_ID ] ) . toBe (
119+ 'summer_promotion_01'
120+ ) ;
121+ expect ( firstEvent . attributes [ Attr . TRAFFIC_SOURCE_TERM ] ) . toBe (
122+ 'running_shoes'
123+ ) ;
124+ expect ( firstEvent . attributes [ Attr . TRAFFIC_SOURCE_CONTENT ] ) . toBe (
125+ 'banner_ad_1'
126+ ) ;
127+ expect ( firstEvent . attributes [ Attr . TRAFFIC_SOURCE_CLID ] ) . toBe (
128+ 'amazon_ad_123'
129+ ) ;
130+ expect ( firstEvent . attributes [ Attr . TRAFFIC_SOURCE_CLID_PLATFORM ] ) . toBe (
131+ 'amazon_ads'
132+ ) ;
133+ } ) ;
134+
80135 test ( 'test record event with name success' , async ( ) => {
81136 const sendRequestMock = jest . spyOn ( NetRequest , 'sendRequest' ) ;
82137 ClickstreamAnalytics . init ( {
@@ -111,7 +166,7 @@ describe('ClickstreamAnalytics test', () => {
111166 name : 'Nature' ,
112167 category : 'book' ,
113168 price : 56.5 ,
114- customKey : " customValue" ,
169+ customKey : ' customValue' ,
115170 } ;
116171 ClickstreamAnalytics . record ( {
117172 name : 'testEvent' ,
@@ -120,6 +175,8 @@ describe('ClickstreamAnalytics test', () => {
120175 longValue : 4232032890992380000 ,
121176 isNew : true ,
122177 score : 85.22 ,
178+ [ Attr . VALUE ] : 56.5 ,
179+ [ Attr . CURRENCY ] : 'USD' ,
123180 } ,
124181 items : [ item ] ,
125182 } ) ;
0 commit comments