Skip to content

Commit

Permalink
setUser tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
M-OC committed Aug 29, 2019
1 parent 4cc5854 commit 55a85ba
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 61 deletions.
7 changes: 7 additions & 0 deletions src/lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,12 @@ export default {
'storage': {
'paypalCrCart': 'paypal-cr-cart',
'paypalCrCartExpiry': 'paypal-cr-cart-expiry'
},
'defaultTrackerConfig': {
'user': {
'id': null,
'email': null,
'name': null
}
}
};
33 changes: 33 additions & 0 deletions src/lib/get-property-id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { getClientID, getMerchantID } from '@paypal/sdk-client/src';

export const getPropertyId = ({ paramsToPropertyIdUrl }) => {
return new Promise(resolve => {
const clientId = getClientID();
const merchantId = getMerchantID()[0];
const propertyIdKey = `property-id-${ clientId }-${ merchantId }`;
const savedPropertyId = window.localStorage.getItem(propertyIdKey);
const currentUrl = `${ window.location.protocol }//${ window.location.host }`;
if (savedPropertyId) {
return resolve(savedPropertyId);
}
let url;
if (paramsToPropertyIdUrl) {
url = paramsToPropertyIdUrl();
} else {
url = 'https://paypal.com/tagmanager/containers/xo';
}
return window.fetch(`${ url }?mrid=${ merchantId }&url=${ encodeURIComponent(currentUrl) }`)
.then(res => {
if (res.status === 200) {
return res;
}
})
.then(r => r.json()).then(container => {
window.localStorage.setItem(propertyIdKey, container.id);
resolve(container.id);
})
.catch(() => {
// doing nothing for now since there's no logging
});
});
};
6 changes: 6 additions & 0 deletions src/lib/track.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ export const track = <T>(config : Config, trackingType : TrackingType, trackingD
...config.user,
id: getUserIdCookie()
};

// remove null, undefined values
user.id || delete user.id
user.email || delete user.email
user.name || delete user.name

const deviceInfo = getDeviceInfo();
const data = {
...trackingData,
Expand Down
54 changes: 15 additions & 39 deletions src/tracker-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getClientID, getMerchantID } from '@paypal/sdk-client/src';

// $FlowFixMe
import { getUserIdCookie } from './lib/cookie-utils';
import { getPropertyId } from './lib/get-property-id';
import getJetlore from './lib/jetlore';
import { composeCart } from './lib/compose-cart';
import { track } from './lib/track';
Expand All @@ -25,7 +26,8 @@ import type {

const {
accessTokenUrl,
storage
storage,
defaultTrackerConfig
} = constants;

const getAccessToken = (url : string, mrid : string) : Promise<Object> => {
Expand Down Expand Up @@ -93,8 +95,6 @@ const getJetlorePayload = (type : string, options : Object) : Object => {

let trackEventQueue = [];

const defaultTrackerConfig = { user: { email: undefined, name: undefined } };

export const clearTrackQueue = (config : Config) => {
// $FlowFixMe
return trackEventQueue.filter(([ trackingType, trackingData ]) => { // eslint-disable-line array-callback-return
Expand Down Expand Up @@ -129,38 +129,6 @@ const clearExpiredCart = () => {
}
};

const getPropertyId = ({ paramsToPropertyIdUrl }) => {
return new Promise(resolve => {
const clientId = getClientID();
const merchantId = getMerchantID()[0];
const propertyIdKey = `property-id-${ clientId }-${ merchantId }`;
const savedPropertyId = window.localStorage.getItem(propertyIdKey);
const currentUrl = `${ window.location.protocol }//${ window.location.host }`;
if (savedPropertyId) {
return resolve(savedPropertyId);
}
let url;
if (paramsToPropertyIdUrl) {
url = paramsToPropertyIdUrl();
} else {
url = 'https://paypal.com/tagmanager/containers/xo';
}
return window.fetch(`${ url }?mrid=${ merchantId }&url=${ encodeURIComponent(currentUrl) }`)
.then(res => {
if (res.status === 200) {
return res;
}
})
.then(r => r.json()).then(container => {
window.localStorage.setItem(propertyIdKey, container.id);
resolve(container.id);
})
.catch(() => {
// doing nothing for now since there's no logging
});
});
};

export const setImplicitPropertyId = (config : Config) => {
/*
** this is used for backwards compatibility
Expand All @@ -182,7 +150,8 @@ const clearCancelledCart = () => {
window.localStorage.removeItem(storage.paypalCrCart);
};

export const Tracker = (config? : Config = defaultTrackerConfig) => {
export const Tracker = (config? : Config = {}) => {
config = { ...defaultTrackerConfig, ...config }
/*
* Use the get param ?ppDebug=true to see logs
*
Expand Down Expand Up @@ -252,12 +221,19 @@ export const Tracker = (config? : Config = defaultTrackerConfig) => {
},
purchase: (data : PurchaseData) => track(config, 'purchase', data),
setUser: (data : UserData) => {
const user = data.user || data
const configUser = config.user || {}

const userId = user.id !== undefined ? user.id : config.user.id
const userEmail = user.email !== undefined ? user.email : config.user.email
const userName = user.name !== undefined ? user.name : config.user.name

config = {
...config,
user: {
...config.user,
email: data.user.email || ((config && config.user) || {}).email,
name: data.user.name || ((config && config.user) || {}).name
id: userId,
email: userEmail,
name: userName
}
};
trackEvent(config, 'setUser', { oldUserId: getUserIdCookie() });
Expand Down
6 changes: 3 additions & 3 deletions test/tracker-component.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -462,9 +462,9 @@ describe('paypal.Tracker', () => {
JSON.stringify({
oldUserId: 'abc123',
user: {
id: 'abc123',
email: '__test__email9',
name: '__test__userName9',
id: 'abc123'
},
propertyId,
trackingType: 'setUser',
Expand Down Expand Up @@ -496,9 +496,9 @@ describe('paypal.Tracker', () => {
JSON.stringify({
oldUserId: 'abc123',
user: {
id: 'abc123',
email: '[email protected]',
name: '__test__name',
id: 'abc123'
},
propertyId,
trackingType: 'setUser',
Expand Down Expand Up @@ -544,9 +544,9 @@ describe('paypal.Tracker', () => {
currencyCode: 'USD',
cartEventType: 'addToCart',
user: {
id: 'abc123',
email: '__test__email2',
name: '__test__name1',
id: 'abc123'
},
propertyId,
trackingType: 'cartEvent',
Expand Down
Loading

0 comments on commit 55a85ba

Please sign in to comment.