Skip to content

Commit

Permalink
fix Analytics initPinpoint error
Browse files Browse the repository at this point in the history
  • Loading branch information
powerful23 committed Jan 16, 2018
1 parent 99acd58 commit 8ba7781
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 40 deletions.
10 changes: 10 additions & 0 deletions packages/aws-amplify/__tests__/Analytics/Analytics-unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ describe("Analytics test", () => {
spyon.mockClear();
});

test("with no config", () => {
const spyon = jest.spyOn(Analytics.prototype, "configure");

const analytics = new Analytics(null);

expect(spyon).not.toBeCalled();

spyon.mockClear();
});

test('with client_info platform', () => {
const spyon = jest.spyOn(Analytics.prototype, "configure");
const spyon2 = jest.spyOn(ClientDevice, 'clientInfo').mockImplementationOnce(() => {
Expand Down
68 changes: 28 additions & 40 deletions packages/aws-amplify/src/Analytics/Analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 +45,15 @@ export default class AnalyticsClass {
* @param config - Configuration of the Analytics
*/
constructor(config: AnalyticsOptions) {
this.configure(config);

if (config) {
this.configure(config);
} else {
this._config = {};
}

const client_info:any = ClientDevice.clientInfo();
if (client_info.platform) { this._config.platform = client_info.platform; }

// store endpointId into localstorage
if (!this._config.endpointId) {
/*
if (window.localStorage) {
let endpointId = window.localStorage.getItem('amplify_endpoint_id');
if (!endpointId) {
endpointId = this.generateRandomString();
window.localStorage.setItem('amplify_endpoint_id', endpointId);
}
this._config.endpointId = endpointId;
}
else {
this._config.endpointId = this.generateRandomString();
}*/
const credentials = this._config.credentials;
if (credentials && credentials.identityId) {
this._config.endpointId = credentials.identityId;
}
}

this._buffer = [];
}

Expand Down Expand Up @@ -128,11 +112,11 @@ export default class AnalyticsClass {
return new Promise<any>((res, rej) => {
this.mobileAnalytics.putEvents(params, (err, data) => {
if (err) {
logger.debug('record event failed. ' + err);
logger.debug('record event failed. ', err);
rej(err);
}
else {
logger.debug('record event success. ' + data);
logger.debug('record event success. ', data);
res(data);
}
});
Expand Down Expand Up @@ -164,11 +148,11 @@ export default class AnalyticsClass {
return new Promise<any>((res, rej) => {
this.mobileAnalytics.putEvents(params, (err, data) => {
if (err) {
logger.debug('record event failed. ' + err);
logger.debug('record event failed. ', err);
rej(err);
}
else {
logger.debug('record event success. ' + data);
logger.debug('record event success. ', data);
res(data);
}
});
Expand All @@ -182,6 +166,7 @@ export default class AnalyticsClass {
*/
async restart() {
this.stopSession().then((data) => {
logger.debug('restarting clients');
return this._initClients();
}).catch(e => {
logger.debug('restart error', e);
Expand Down Expand Up @@ -224,11 +209,11 @@ export default class AnalyticsClass {
return new Promise<any>((res, rej) => {
this.mobileAnalytics.putEvents(params, (err, data) => {
if (err) {
logger.debug('record event failed. ' + err);
logger.debug('record event failed. ', err);
rej(err);
}
else {
logger.debug('record event success. ' + data);
logger.debug('record event success. ', data);
res(data);
}
});
Expand Down Expand Up @@ -313,20 +298,19 @@ export default class AnalyticsClass {
* check if current crednetials exists
*/
_ensureCredentials() {
const conf = this._config;

// commented
// will cause bug if another user logged in without refreshing page
// if (conf.credentials) { return Promise.resolve(true); }

return Auth.currentCredentials()
.then(credentials => {
const cred = Auth.essentialCredentials(credentials);
logger.debug('set credentials for analytics', cred);
conf.credentials = cred;

if (!conf.endpointId && conf.credentials) {
conf.endpointId = conf.credentials.identityId;
}

this._config.credentials = cred;
this._config.endpointId = this._config.credentials.identityId;
logger.debug('set endpointId for analytics', this._config.endpointId);
logger.debug('set credentials for analytics', this._config.credentials);

return true;
})
Expand All @@ -349,9 +333,13 @@ export default class AnalyticsClass {
if (!credentialsOK) { return false; }

this._initMobileAnalytics();
await this._initPinpoint();
this.startSession();

try {
await this._initPinpoint();
this.startSession();
} catch (e) {
return false;
}

return true;
}

Expand Down Expand Up @@ -392,7 +380,7 @@ export default class AnalyticsClass {
EndpointId: endpointId,
EndpointRequest: request
};
logger.debug(update_params);
logger.debug('updateEndpoint with params: ', update_params);

return new Promise((res, rej) => {
this.pinpointClient.updateEndpoint(update_params, function(err, data) {
Expand All @@ -415,7 +403,7 @@ export default class AnalyticsClass {
const client_info: any = ClientDevice.clientInfo();
const credentials = this._config.credentials;
const user_id = (credentials && credentials.authenticated) ? credentials.identityId : null;
logger.debug('demographic user id: ' + user_id);
logger.debug('demographic user id: ', user_id);
return {
Demographic: {
AppVersion: this._config.appVersion || client_info.appVersion,
Expand Down

0 comments on commit 8ba7781

Please sign in to comment.