Skip to content

Commit

Permalink
Merge pull request #855 from powerful23/analytics-client-context-conf…
Browse files Browse the repository at this point in the history
…igurable

Analytics client context configurable
powerful23 authored May 18, 2018

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents 19fb115 + 1c4a423 commit bb1e27a
Showing 4 changed files with 66 additions and 9 deletions.
15 changes: 14 additions & 1 deletion docs/media/analytics_guide.md
Original file line number Diff line number Diff line change
@@ -59,7 +59,20 @@ Amplify.configure({
// OPTIONAL - Customized endpoint
endpointId: 'XXXXXXXXXXXX',
// OPTIONAL - disable Analytics if true
disabled: false
disabled: false,
// OPTIONAL - client context
clientContext: {
clientId: 'xxxxx',
appTitle: 'xxxxx',
appVersionName: 'xxxxx',
appVersionCode: 'xxxxx',
appPackageName: 'xxxxx',
platform: 'xxxxx',
platformVersion: 'xxxxx',
model: 'xxxxx',
make: 'xxxxx',
locale: 'xxxxx'
}
}
});
```
Original file line number Diff line number Diff line change
@@ -166,6 +166,35 @@ describe("AnalyticsProvider test", () => {
spyon.mockClear();
});

test('custom events with client context', async () => {
const analytics = new AnalyticsProvider();
analytics.configure({
clientContext: {
clientId: 'xxxxx',
appTitle: 'xxxxx',
appVersionName: 'xxxxx',
appVersionCode: 'xxxxx',
appPackageName: 'xxxxx',
platform: 'xxxxx',
platformVersion: 'xxxxx',
model: 'xxxxx',
make: 'xxxxx',
locale: 'xxxxx'
}
})
const spyon = jest.spyOn(MobileAnalytics.prototype, 'putEvents').mockImplementationOnce((params, callback) => {
callback(null, 'data');
});

const params = {eventName: 'custom event', config: options, timestamp};
await analytics.record(params);
expect(spyon).toBeCalled();
expect(spyon.mock.calls[0][0].events[0].eventType).toBe('custom event');

await analytics.record(params);
spyon.mockClear();
});

test('custom event error', async () => {
const analytics = new AnalyticsProvider();
const spyon = jest.spyOn(MobileAnalytics.prototype, 'putEvents').mockImplementationOnce((params, callback) => {
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@
* and limitations under the License.
*/
import { ConsoleLogger as Logger, Pinpoint, MobileAnalytics} from '../../Common';
import Platform from '../../Common/Platform';
import Cache from '../../Cache';
import { AnalyticsProvider } from '../types';
import { v1 as uuid } from 'uuid';
@@ -368,17 +369,31 @@ export default class AWSAnalyticsProvider implements AnalyticsProvider {
* generate client context with endpoint Id and app Id provided
*/
private _generateClientContext() {
const { endpointId, appId } = this._config;
const clientContext = {
const { endpointId, appId, clientInfo } = this._config;
const clientContext = this._config.clientContext || {};

const clientCtx = {
client: {
client_id: endpointId
client_id: clientContext.clientId || endpointId,
app_title: clientContext.appTitle,
app_version_name: clientContext.appVersionName,
app_version_code: clientContext.appVersionCode,
app_package_name: clientContext.appPackageName,
},
env: {
platform: clientContext.platform || clientInfo.platform,
platform_version: clientContext.platformVersion || clientInfo.version,
model: clientContext.model || clientInfo.model,
make: clientContext.make || clientInfo.make,
locale: clientContext.locale
},
services: {
mobile_analytics: {
app_id: appId
app_id: appId,
sdk_name: Platform.userAgent
}
}
};
return JSON.stringify(clientContext);
return JSON.stringify(clientCtx);
}
}
6 changes: 3 additions & 3 deletions packages/aws-amplify/src/Common/Platform/index.ts
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@
*/

const Platform = {
'userAgent': 'aws-amplify/0.1.x js',
'userAgent': 'aws-amplify/0.4.x js',
'product': '',
'navigator': null,
'isReactNative': false
@@ -22,11 +22,11 @@ if (typeof navigator !== 'undefined' && navigator.product) {
Platform.navigator = navigator || null;
switch(navigator.product) {
case 'ReactNative':
Platform.userAgent = 'aws-amplify/0.1.x react-native';
Platform.userAgent = 'aws-amplify/0.4.x react-native';
Platform.isReactNative = true;
break;
default:
Platform.userAgent = 'aws-amplify/0.1.x js';
Platform.userAgent = 'aws-amplify/0.4.x js';
Platform.isReactNative = false;
break;
}

0 comments on commit bb1e27a

Please sign in to comment.