Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Analytics client context configurable #855

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion docs/media/analytics_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
});
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we default some of these to what's detected in Platform? For example platform could be navigator.product or browser...etc (if available), perhaps use the common/Platform class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using clientInfo to get the default info.

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
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down