@@ -350,13 +350,33 @@ function initialize(env, user, options) {
350350 } ) ;
351351 }
352352
353+ function refreshGoalTracker ( ) {
354+ if ( goalTracker ) {
355+ goalTracker . dispose ( ) ;
356+ }
357+ if ( goals && goals . length ) {
358+ goalTracker = GoalTracker ( goals , sendGoalEvent ) ;
359+ }
360+ }
361+
362+ function attachHistoryListeners ( ) {
363+ if ( ! ! ( window . history && history . pushState ) ) {
364+ window . removeEventListener ( 'popstate' , refreshGoalTracker ) ;
365+ window . addEventListener ( 'popstate' , refreshGoalTracker ) ;
366+ } else {
367+ window . removeEventListener ( 'hashchange' , refreshGoalTracker ) ;
368+ window . addEventListener ( 'hashchange' , refreshGoalTracker ) ;
369+ }
370+ }
371+
353372 requestor . fetchGoals ( function ( err , g ) {
354373 if ( err ) {
355374 emitter . maybeReportError ( new errors . LDUnexpectedResponseError ( 'Error fetching goals: ' + err . message ? err . message : err ) ) ;
356375 }
357376 if ( g && g . length > 0 ) {
358377 goals = g ;
359378 goalTracker = GoalTracker ( goals , sendGoalEvent ) ;
379+ attachHistoryListeners ( ) ;
360380 }
361381 } ) ;
362382
@@ -379,23 +399,6 @@ function initialize(env, user, options) {
379399 events . flush ( ident . getUser ( ) , true ) ;
380400 } ) ;
381401
382- function refreshGoalTracker ( ) {
383- if ( goalTracker ) {
384- goalTracker . dispose ( ) ;
385- }
386- if ( goals && goals . length ) {
387- goalTracker = GoalTracker ( goals , sendGoalEvent ) ;
388- }
389- }
390-
391- if ( goals && goals . length > 0 ) {
392- if ( ! ! ( window . history && history . pushState ) ) {
393- window . addEventListener ( 'popstate' , refreshGoalTracker ) ;
394- } else {
395- window . addEventListener ( 'hashchange' , refreshGoalTracker ) ;
396- }
397- }
398-
399402 window . addEventListener ( 'message' , handleMessage ) ;
400403
401404 var readyPromise = new Promise ( function ( resolve ) {
0 commit comments