@@ -2566,6 +2566,8 @@ describe('setVersionName', function() {
25662566
25672567 afterEach ( function ( ) {
25682568 reset ( ) ;
2569+ cookie . remove ( '__utmz' ) ;
2570+ cookie . reset ( ) ;
25692571 clock . restore ( ) ;
25702572 } ) ;
25712573
@@ -2701,6 +2703,136 @@ describe('setVersionName', function() {
27012703 }
27022704 } ) ;
27032705 } ) ;
2706+
2707+ it ( 'should allow utm parameters to unset upon instantiating a new session' , function ( done ) {
2708+ reset ( ) ;
2709+ // send first $identify call with UTM params
2710+ sinon . stub ( amplitude , '_getUrlParams' ) . returns ( '?utm_source=google&utm_campaign=(organic)&utm_medium=organic&utm_term=(none)&utm_content=link' ) ;
2711+ amplitude . init ( apiKey , undefined , { includeUtm : true , saveParamsReferrerOncePerSession : false , unsetParamsReferrerOnNewSession : true } ) ;
2712+
2713+ // advance clock to force new session
2714+ clock . tick ( 30 * 60 * 1000 + 1 ) ;
2715+ amplitude . _getUrlParams . restore ( ) ;
2716+
2717+ // send new session events
2718+ amplitude . init ( apiKey , undefined , { includeUtm : true , saveParamsReferrerOncePerSession : false , unsetParamsReferrerOnNewSession : true } ) ;
2719+ amplitude . logEvent ( 'UTM Test Event' , { } ) ;
2720+
2721+ // ensure the server has responded
2722+ server . respondWith ( 'success' ) ;
2723+ server . respond ( ) ;
2724+
2725+ var firstSessionEvents = JSON . parse ( querystring . parse ( server . requests [ 0 ] . requestBody ) . e ) ;
2726+ var secondSessionEvents = JSON . parse ( querystring . parse ( server . requests [ 1 ] . requestBody ) . e ) ;
2727+ var firstSessionInit = firstSessionEvents [ 0 ] ;
2728+ var secondSessionInit = secondSessionEvents [ 0 ] ;
2729+ var secondSessionEvent = secondSessionEvents [ 1 ] ;
2730+
2731+ assert . equal ( firstSessionInit . event_type , '$identify' , 'should correctly called $identify' ) ;
2732+ assert . deepEqual ( firstSessionInit . user_properties , {
2733+ $setOnce : {
2734+ initial_utm_source : "google" ,
2735+ initial_utm_medium : "organic" ,
2736+ initial_utm_campaign : "(organic)" ,
2737+ initial_utm_term : "(none)" ,
2738+ initial_utm_content : "link"
2739+ } ,
2740+ $set : {
2741+ utm_source : "google" ,
2742+ utm_medium : "organic" ,
2743+ utm_campaign : "(organic)" ,
2744+ utm_term : "(none)" ,
2745+ utm_content : "link"
2746+ }
2747+ } , 'should call $identify to set the correct UTM params' ) ;
2748+ assert . equal ( secondSessionInit . event_type , '$identify' , 'should have re-called $identify to unset utm params upon a new session' ) ;
2749+ assert . deepEqual ( secondSessionInit . user_properties , {
2750+ '$unset' : {
2751+ referrer : "-" ,
2752+ utm_source :"-" ,
2753+ utm_medium :"-" ,
2754+ utm_campaign :"-" ,
2755+ utm_term :"-" ,
2756+ utm_content :"-"
2757+ }
2758+ } , 'should correctly unset UTM params' ) ;
2759+ assert . deepEqual ( secondSessionEvent . user_properties , { } , 'should correctly unset UTM params upon a new session' ) ;
2760+ done ( )
2761+ } ) ;
2762+
2763+ it ( 'should reset utm parameters if it has changed during a new session' , function ( done ) {
2764+ reset ( ) ;
2765+ // send first $identify call with UTM params
2766+ sinon . stub ( amplitude , '_getUrlParams' ) . returns ( '?utm_source=google&utm_campaign=(organic)&utm_medium=organic&utm_term=(none)&utm_content=link' ) ;
2767+ amplitude . init ( apiKey , undefined , { includeUtm : true , saveParamsReferrerOncePerSession : false , unsetParamsReferrerOnNewSession : true } ) ;
2768+
2769+ // advance clock to force new session, enter through a different campaign
2770+ clock . tick ( 30 * 60 * 1000 + 1 ) ;
2771+ amplitude . _getUrlParams . restore ( ) ;
2772+ sinon . stub ( amplitude , '_getUrlParams' ) . returns ( '?utm_source=google&utm_campaign=(mail_promotion)&utm_medium=email&utm_term=(none)&utm_content=click' ) ;
2773+ amplitude . init ( apiKey , undefined , { includeUtm : true , saveParamsReferrerOncePerSession : false , unsetParamsReferrerOnNewSession : true } ) ;
2774+
2775+ // send new session events
2776+ amplitude . init ( apiKey , undefined , { includeUtm : true , saveParamsReferrerOncePerSession : false , unsetParamsReferrerOnNewSession : true } ) ;
2777+ amplitude . logEvent ( 'UTM Test Event' , { } ) ;
2778+
2779+ // ensure the server has responded
2780+ server . respondWith ( 'success' ) ;
2781+ server . respond ( ) ;
2782+
2783+ amplitude . _getUrlParams . restore ( ) ;
2784+ var firstSessionEvents = JSON . parse ( querystring . parse ( server . requests [ 0 ] . requestBody ) . e ) ;
2785+ var secondSessionEvents = JSON . parse ( querystring . parse ( server . requests [ 1 ] . requestBody ) . e ) ;
2786+ var firstSessionInit = firstSessionEvents [ 0 ] ;
2787+ var secondSessionInit = secondSessionEvents [ 0 ] ;
2788+ var secondSessionEvent = secondSessionEvents [ 1 ] ;
2789+
2790+ assert . equal ( firstSessionInit . event_type , '$identify' , 'should correctly called $identify' ) ;
2791+ assert . deepEqual ( firstSessionInit . user_properties , {
2792+ $setOnce : {
2793+ initial_utm_source : "google" ,
2794+ initial_utm_medium : "organic" ,
2795+ initial_utm_campaign : "(organic)" ,
2796+ initial_utm_term : "(none)" ,
2797+ initial_utm_content : "link"
2798+ } ,
2799+ $set : {
2800+ utm_source : "google" ,
2801+ utm_medium : "organic" ,
2802+ utm_campaign : "(organic)" ,
2803+ utm_term : "(none)" ,
2804+ utm_content : "link"
2805+ }
2806+ } , 'should call $identify to set the correct UTM params' ) ;
2807+ assert . equal ( secondSessionInit . event_type , '$identify' , 'should have re-called $identify to unset utm params upon a new session' ) ;
2808+ assert . deepEqual ( secondSessionInit . user_properties , {
2809+ '$unset' : {
2810+ referrer : "-" ,
2811+ utm_source :"-" ,
2812+ utm_medium :"-" ,
2813+ utm_campaign :"-" ,
2814+ utm_term :"-" ,
2815+ utm_content :"-"
2816+ }
2817+ } , 'should correctly unset UTM params' ) ;
2818+ assert . deepEqual ( secondSessionEvent . user_properties , {
2819+ $setOnce : {
2820+ initial_utm_source : "google" ,
2821+ initial_utm_medium : "email" ,
2822+ initial_utm_campaign : "(mail_promotion)" ,
2823+ initial_utm_term : "(none)" ,
2824+ initial_utm_content : "click"
2825+ } ,
2826+ "$set" : {
2827+ "utm_source" : "google" ,
2828+ "utm_medium" : "email" ,
2829+ "utm_campaign" : "(mail_promotion)" ,
2830+ "utm_term" : "(none)" ,
2831+ "utm_content" : "click"
2832+ }
2833+ } , 'should correctly set new UTM params upon a new session' ) ;
2834+ done ( )
2835+ } ) ;
27042836 } ) ;
27052837
27062838 describe ( 'gatherReferrer' , function ( ) {
0 commit comments