diff --git a/docs/media/analytics_guide.md b/docs/media/analytics_guide.md index ad0c8b54d17..426ad8a17d4 100644 --- a/docs/media/analytics_guide.md +++ b/docs/media/analytics_guide.md @@ -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' + } } }); ``` diff --git a/packages/aws-amplify/__tests__/Analytics/Providers/AWSAnalyticsProvider-unit-test.ts b/packages/aws-amplify/__tests__/Analytics/Providers/AWSAnalyticsProvider-unit-test.ts index 73189c57bd7..77722495136 100644 --- a/packages/aws-amplify/__tests__/Analytics/Providers/AWSAnalyticsProvider-unit-test.ts +++ b/packages/aws-amplify/__tests__/Analytics/Providers/AWSAnalyticsProvider-unit-test.ts @@ -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) => { diff --git a/packages/aws-amplify/src/Analytics/Providers/AWSAnalyticsProvider.ts b/packages/aws-amplify/src/Analytics/Providers/AWSAnalyticsProvider.ts index e8090fc754f..6f321f14898 100644 --- a/packages/aws-amplify/src/Analytics/Providers/AWSAnalyticsProvider.ts +++ b/packages/aws-amplify/src/Analytics/Providers/AWSAnalyticsProvider.ts @@ -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); } } diff --git a/packages/aws-amplify/src/Common/Platform/index.ts b/packages/aws-amplify/src/Common/Platform/index.ts index 97fcaf332df..1eed1cf7648 100644 --- a/packages/aws-amplify/src/Common/Platform/index.ts +++ b/packages/aws-amplify/src/Common/Platform/index.ts @@ -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; }