Skip to content

Commit

Permalink
feat(analytics): Use custom user agent in all Analytics providers (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
stocaaro committed May 27, 2023
1 parent 9f290d5 commit 9fe4a5e
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 10 deletions.
70 changes: 70 additions & 0 deletions packages/analytics/__tests__/Providers/CustomUserAgent.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import {
AmazonPersonalizeProvider,
AWSKinesisFirehoseProvider,
AWSKinesisProvider,
AWSPinpointProvider,
} from '../../src/Providers';

describe('Each provider client is configured with the custom user client', () => {
describe('AmazonPersonalizeProvider', () => {
test('received the custom user client', () => {
const provider = new AmazonPersonalizeProvider();
// Run init to setup the client
provider['_init']({ region: 'us-east-1' }, {});

expect(
provider['_personalize']['config']['customUserAgent']
).toMatchObject([
['aws-amplify', expect.any(String)],
['analytics', '1'],
['framework', '0'],
]);
});
});

describe('AWSKinesisFirehoseProvider', () => {
test('received the custom user client', () => {
const provider = new AWSKinesisFirehoseProvider();
// Run init to setup the client
provider['_init']({ region: 'us-east-1' }, {});

expect(
provider['_kinesisFirehose']['config']['customUserAgent']
).toMatchObject([
['aws-amplify', expect.any(String)],
['analytics', '1'],
['framework', '0'],
]);
});
});

describe('AWSKinesisProvider', () => {
test('received the custom user client', () => {
const provider = new AWSKinesisProvider();
// Run init to setup the client
provider['_init']({ region: 'us-east-1' }, {});

expect(provider['_kinesis']['config']['customUserAgent']).toMatchObject([
['aws-amplify', expect.any(String)],
['analytics', '1'],
['framework', '0'],
]);
});
});

describe('AWSPinpointProvider', () => {
test('received the custom user client', () => {
const provider = new AWSPinpointProvider({ region: 'us-east-1' });
// Run init to setup the client
provider['_initClients']({});

expect(
provider['pinpointClient']['config']['customUserAgent']
).toMatchObject([
['aws-amplify', expect.any(String)],
['analytics', '1'],
['framework', '0'],
]);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
FirehoseClient,
} from '@aws-sdk/client-firehose';
import { fromUtf8 } from '@aws-sdk/util-utf8-browser';
import { getAnalyticsUserAgent } from '../utils/UserAgent';

const logger = new Logger('AWSKineisFirehoseProvider');

Expand Down Expand Up @@ -97,6 +98,7 @@ export class AWSKinesisFirehoseProvider extends AWSKinesisProvider {
apiVersion: '2015-08-04',
region,
credentials,
customUserAgent: getAnalyticsUserAgent(),
});
return true;
}
Expand Down
9 changes: 3 additions & 6 deletions packages/analytics/src/Providers/AWSKinesisProvider.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import {
ConsoleLogger as Logger,
Credentials,
getAmplifyUserAgent,
} from '@aws-amplify/core';
import { ConsoleLogger as Logger, Credentials } from '@aws-amplify/core';
import { KinesisClient, PutRecordsCommand } from '@aws-sdk/client-kinesis';
import { AnalyticsProvider } from '../types';
import { fromUtf8 } from '@aws-sdk/util-utf8-browser';
import { getAnalyticsUserAgent } from '../utils/UserAgent';

const logger = new Logger('AWSKinesisProvider');

Expand Down Expand Up @@ -224,7 +221,7 @@ export class AWSKinesisProvider implements AnalyticsProvider {
this._kinesis = new KinesisClient({
region,
credentials,
customUserAgent: getAmplifyUserAgent(),
customUserAgent: getAnalyticsUserAgent(),
endpoint,
});
return true;
Expand Down
4 changes: 2 additions & 2 deletions packages/analytics/src/Providers/AWSPinpointProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
Credentials,
Signer,
Hub,
getAmplifyUserAgent,
transferKeyToLowerCase,
transferKeyToUpperCase,
} from '@aws-amplify/core';
Expand All @@ -30,6 +29,7 @@ import {
} from '../types';
import { v1 as uuid } from 'uuid';
import EventsBuffer from './EventBuffer';
import { getAnalyticsUserAgent } from '../utils/UserAgent';

const AMPLIFY_SYMBOL = (
typeof Symbol !== 'undefined' && typeof Symbol.for === 'function'
Expand Down Expand Up @@ -517,7 +517,7 @@ export class AWSPinpointProvider implements AnalyticsProvider {
this.pinpointClient = new PinpointClient({
region,
credentials,
customUserAgent: getAmplifyUserAgent(),
customUserAgent: getAnalyticsUserAgent(),
});

// TODO: remove this middleware once a long term fix is implemented by aws-sdk-js team.
Expand Down
4 changes: 2 additions & 2 deletions packages/analytics/src/Providers/AmazonPersonalizeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import {
ConsoleLogger as Logger,
Credentials,
getAmplifyUserAgent,
browserOrNode,
} from '@aws-amplify/core';
import {
Expand All @@ -23,6 +22,7 @@ import get from 'lodash/get';
import isEmpty from 'lodash/isEmpty';
import isEqual from 'lodash/isEqual';
import { AnalyticsProvider } from '../types';
import { getAnalyticsUserAgent } from '../utils/UserAgent';

const logger = new Logger('AmazonPersonalizeProvider');

Expand Down Expand Up @@ -372,7 +372,7 @@ export class AmazonPersonalizeProvider implements AnalyticsProvider {
this._personalize = new PersonalizeEventsClient({
region,
credentials,
customUserAgent: getAmplifyUserAgent(),
customUserAgent: getAnalyticsUserAgent(),
});
return true;
}
Expand Down
12 changes: 12 additions & 0 deletions packages/analytics/src/utils/UserAgent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {
AnalyticsAction,
Category,
getAmplifyUserAgent,
} from '@aws-amplify/core';

export function getAnalyticsUserAgent() {
return getAmplifyUserAgent({
category: Category.Analytics,
action: AnalyticsAction.Record,
});
}
6 changes: 6 additions & 0 deletions packages/core/src/Platform/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export enum Framework {
export enum Category {
API = 'api',
Auth = 'auth',
Analytics = 'analytics',
DataStore = 'datastore',
Geo = 'geo',
Interactions = 'interactions',
Expand All @@ -40,6 +41,9 @@ export enum Category {

// Actions
/* TODO: Replace 'None' with all expected Actions */
export enum AnalyticsAction {
Record = '1',
}
export enum ApiAction {
None = '0',
}
Expand Down Expand Up @@ -74,6 +78,7 @@ export enum StorageAction {
type ActionMap = {
[Category.Auth]: AuthAction;
[Category.API]: ApiAction;
[Category.Analytics]: AnalyticsAction;
[Category.DataStore]: DataStoreAction;
[Category.Geo]: GeoAction;
[Category.Interactions]: InteractionsAction;
Expand All @@ -98,6 +103,7 @@ export type CustomUserAgentDetails =
| (CustomUserAgentDetailsBase & { category?: never; action?: never })
| UserAgentDetailsWithCategory<Category.API>
| UserAgentDetailsWithCategory<Category.Auth>
| UserAgentDetailsWithCategory<Category.Analytics>
| UserAgentDetailsWithCategory<Category.DataStore>
| UserAgentDetailsWithCategory<Category.Geo>
| UserAgentDetailsWithCategory<Category.Interactions>
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export {
export {
ApiAction,
AuthAction,
AnalyticsAction,
Category,
CustomUserAgentDetails,
DataStoreAction,
Expand Down

0 comments on commit 9fe4a5e

Please sign in to comment.