Skip to content

Commit

Permalink
sync Analytics change with aws amplify
Browse files Browse the repository at this point in the history
  • Loading branch information
powerful23 committed Jan 18, 2018
1 parent dcfcc06 commit f27c388
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 74 deletions.
61 changes: 25 additions & 36 deletions packages/aws-amplify-react-native/dist/Analytics/Analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,27 @@ import Auth from '../Auth';

const logger = new Logger('AnalyticsClass');
const NON_RETRYABLE_EXCEPTIONS = ['BadRequestException', 'SerializationException', 'ValidationException'];

/**
* Provide mobile analytics client functions
*/
export default class AnalyticsClass {

/**
* Initialize Analtyics
* @param config - Configuration of the Analytics
*/
constructor(config) {
this.configure(config);
if (config) {
this.configure(config);
} else {
this._config = {};
}

const client_info = 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 @@ -112,10 +96,10 @@ export default class AnalyticsClass {
return new Promise((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 @@ -145,10 +129,10 @@ export default class AnalyticsClass {
return new Promise((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 @@ -162,6 +146,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 @@ -202,10 +187,10 @@ export default class AnalyticsClass {
return new Promise((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 @@ -297,12 +282,12 @@ export default class AnalyticsClass {

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

conf.credentials = cred;
conf.endpointId = conf.credentials.identityId;

if (!conf.endpointId && conf.credentials) {
conf.endpointId = conf.credentials.identityId;
}
logger.debug('set endpointId for analytics', conf.endpointId);
logger.debug('set credentials for analytics', conf.credentials);

return true;
}).catch(err => {
Expand All @@ -328,8 +313,12 @@ export default class AnalyticsClass {
}

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

return true;
}
Expand Down Expand Up @@ -370,7 +359,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 @@ -393,7 +382,7 @@ export default class AnalyticsClass {
const client_info = 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
66 changes: 28 additions & 38 deletions packages/aws-amplify-react-native/src/Analytics/Analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,25 @@ import Auth from '../Auth';

const logger = new Logger('AnalyticsClass');
const NON_RETRYABLE_EXCEPTIONS = ['BadRequestException', 'SerializationException', 'ValidationException'];

/**
* Provide mobile analytics client functions
*/
export default class AnalyticsClass {

/**
* Initialize Analtyics
* @param config - Configuration of the Analytics
*/
constructor(config) {
this.configure(config);

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

const client_info = 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 @@ -116,11 +100,11 @@ export default class AnalyticsClass {
return new Promise((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 @@ -152,11 +136,11 @@ export default class AnalyticsClass {
return new Promise((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 @@ -170,6 +154,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 @@ -212,11 +197,11 @@ export default class AnalyticsClass {
return new Promise((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 @@ -309,12 +294,12 @@ export default class AnalyticsClass {
return Auth.currentCredentials()
.then(credentials => {
const cred = Auth.essentialCredentials(credentials);
logger.debug('set credentials for analytics', cred);

conf.credentials = cred;
conf.endpointId = conf.credentials.identityId;

if (!conf.endpointId && conf.credentials) {
conf.endpointId = conf.credentials.identityId;
}
logger.debug('set endpointId for analytics', conf.endpointId);
logger.debug('set credentials for analytics', conf.credentials);

return true;
})
Expand All @@ -337,9 +322,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 @@ -380,7 +369,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 @@ -403,7 +392,7 @@ export default class AnalyticsClass {
const client_info = 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 All @@ -416,3 +405,4 @@ export default class AnalyticsClass {
};
}
}

0 comments on commit f27c388

Please sign in to comment.