Skip to content

Commit

Permalink
PWA-1703 Adjust tracking duplicates and checkout flow
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbridge committed Feb 15, 2019
1 parent 9eb0624 commit bf80fcb
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 17 deletions.
8 changes: 3 additions & 5 deletions libraries/tracking/streams/checkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { navigate$ } from '@shopgate/pwa-common/streams/router';
import { isUserLoggedIn } from '@shopgate/pwa-common/selectors/user';
import { CHECKOUT_PATH } from '@shopgate/pwa-common/constants/RoutePaths';

const actionsWl = [ACTION_PUSH, ACTION_REPLACE];

export const checkoutDidEnter$ = navigate$
.filter(({ action, getState }) => {
if (action.params.pathname !== CHECKOUT_PATH) {
Expand All @@ -21,9 +23,5 @@ export const checkoutDidEnter$ = navigate$
* A checkout route can be pushed when a logged in user opens the checkout. It can also replace
* the current route when a user is redirected from the login form after a successful login.
*/
if ([ACTION_PUSH, ACTION_REPLACE].includes(navigateAction)) {
return true;
}

return false;
return actionsWl.includes(navigateAction);
});
19 changes: 15 additions & 4 deletions libraries/tracking/streams/pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ import {
ITEM_WRITE_REVIEW_PATTERN,
} from '@shopgate/pwa-common-commerce/product/constants';
import { FAVORITES_PATH } from '@shopgate/pwa-common-commerce/favorites/constants';
import { PWA_DID_APPEAR } from '../constants';
import {
pwaDidAppear$,
pwaDidDisappear$,
pwaVisibility$,
} from './app';
import { PWA_DID_APPEAR } from '../constants';
import { checkoutDidEnter$ } from './checkout';

/**
* A blacklist of paths that should be tracked within their individual subscriptions.
Expand All @@ -41,17 +43,26 @@ export const blacklistedPatterns = [
ITEM_WRITE_REVIEW_PATTERN,
];

const latestAppActions$ = appWillStart$.merge(pwaVisibility$, checkoutDidEnter$);

/**
* Route did enter and PWA is visible and route is not blacklisted
* @type {Rx.Observable<*[]>}
*/
const routeDidEnterForVisiblePwa$ = routeDidEnter$
.withLatestFrom(appWillStart$.merge(pwaVisibility$))
.withLatestFrom(latestAppActions$)
.filter(([, { action: { type } }]) => type === APP_WILL_START || type === PWA_DID_APPEAR)
.map(([routeDidEnter]) => routeDidEnter);

/**
* Emits when one of the tracked paths is entered or pwa appeared
* PWA reappear after disappear
* @type {Rx.Observable<any>}
*/
const pwaDidAppearAfterDisappear = pwaDidDisappear$.switchMap(() => pwaDidAppear$.first());

/**
* Emits when one of the tracked paths is entered or pwa reappear
*/
export const pagesAreReady$ = routeDidEnterForVisiblePwa$.merge(pwaDidAppear$)
export const pagesAreReady$ = routeDidEnterForVisiblePwa$
.merge(pwaDidAppearAfterDisappear)
.filter(({ action }) => !blacklistedPatterns.find(pattern => action.route.pattern === pattern));
44 changes: 36 additions & 8 deletions libraries/tracking/streams/pages.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { combineReducers } from 'redux';
import { ACTION_REPLACE } from '@virtuous/conductor';
import { createMockStore } from '@shopgate/pwa-common/store';
import { routeDidEnter } from '@shopgate/pwa-common/action-creators/router';
import user from '@shopgate/pwa-common/reducers/user';
import { routeDidEnter, navigate } from '@shopgate/pwa-common/action-creators/router';
import { appWillStart } from '@shopgate/pwa-common/action-creators/app';
import { CHECKOUT_PATH } from '@shopgate/pwa-common/constants/RoutePaths';
import {
pwaDidAppear,
pwaDidDisappear,
Expand Down Expand Up @@ -35,18 +39,19 @@ describe('Page streams', () => {
jest.clearAllMocks();

mockedPattern = '';
({ dispatch } = createMockStore());

// Simulate app is started
dispatch(appWillStart('/'));
({ dispatch } = createMockStore(combineReducers({ user })));

pagesAreReadySubscriber = jest.fn();
pagesAreReady$.subscribe(pagesAreReadySubscriber);
});

describe('pagesAreReady$', () => {
it('should emit when a route not blacklisted', () => {
beforeEach(() => {
// Simulate app is started
dispatch(appWillStart('/'));
});

it('should emit when a route not blacklisted', () => {
dispatch(routeDidEnterWrapped('/somepath'));
expect(pagesAreReadySubscriber).toHaveBeenCalledTimes(1);
});
Expand All @@ -70,16 +75,39 @@ describe('Page streams', () => {
});

describe('coming back from legacy pages', () => {
it('should emit when pwaDidAppear is dispatched and a not blacklisted route is active', () => {
beforeEach(() => {
// Simulate app is started
dispatch(appWillStart('/'));
});
it('should emit when PWA is reappear and a not blacklisted route is active', () => {
dispatch(routeDidEnterWrapped('/somepath'));
dispatch(pwaDidDisappear());
dispatch(pwaDidAppear());
expect(pagesAreReadySubscriber).toHaveBeenCalledTimes(1);
expect(pagesAreReadySubscriber).toHaveBeenCalledTimes(2);
});

it('should not emit when pwaDidAppear is dispatched and a blacklisted is active', () => {
dispatch(routeDidEnterWrapped(blacklistedPatterns[0]));
dispatch(pwaDidDisappear());
dispatch(pwaDidAppear());
expect(pagesAreReadySubscriber).not.toHaveBeenCalled();
});
});

describe('go to checkout', () => {
beforeEach(() => {
// Simulate app is started
dispatch(appWillStart('/'));
});
it('should not emit events when user goes to checkout', () => {
dispatch(routeDidEnterWrapped('/somepath'));
dispatch(navigate({
action: ACTION_REPLACE,
pathname: CHECKOUT_PATH,
}));
// should not track this path
dispatch(routeDidEnterWrapped('/somepath2'));
expect(pagesAreReadySubscriber).toHaveBeenCalledTimes(1);
});
});
});

0 comments on commit bf80fcb

Please sign in to comment.