Skip to content

Commit

Permalink
Merge branch 'next/main' into fix-geo-export
Browse files Browse the repository at this point in the history
  • Loading branch information
jimblanc committed Sep 22, 2023
2 parents 89b0fb6 + f70b9fe commit f3dad3b
Show file tree
Hide file tree
Showing 15 changed files with 145 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { updateEndpoint } from '@aws-amplify/core/internals/providers/pinpoint';
import { identifyUser } from '../../../../src/providers/pinpoint/apis';
import { IdentifyUserParameters } from '../../../../src/types';
import { IdentifyUserInput } from '../../../../src/providers/pinpoint/types';
import {
resolveConfig,
resolveCredentials,
Expand Down Expand Up @@ -42,21 +42,44 @@ describe('Analytics Pinpoint Provider API: identifyUser', () => {
});

it('passes through parameter along with Analytics boilerplate to core Pinpoint identifyUser API', async () => {
const params: IdentifyUserParameters = {
const input: IdentifyUserInput = {
userId: 'user-id',
userProfile: {
attributes: {
customProperties: {
hobbies: ['biking', 'climbing'],
},
email: 'email',
name: 'name',
plan: 'plan',
},
};
await identifyUser(params);
await identifyUser(input);
expect(mockUpdateEndpoint).toBeCalledWith({
...params,
...input,
...credentials,
...config,
category: 'Analytics',
userAgentValue,
});
});

it('passes through service options along with Analytics boilerplate to core Pinpoint identifyUser API', async () => {
const userAttributes = { hobbies: ['biking', 'climbing'] };
const input: IdentifyUserInput = {
userId: 'user-id',
userProfile: {},
};
const options: IdentifyUserInput['options'] = {
serviceOptions: { userAttributes },
};
await identifyUser({ ...input, options });
expect(mockUpdateEndpoint).toBeCalledWith({
...input,
...credentials,
...config,
category: 'Analytics',
userAgentValue,
userAttributes,
});
});
});
21 changes: 13 additions & 8 deletions packages/analytics/src/providers/pinpoint/apis/identifyUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,34 @@ import { resolveConfig, resolveCredentials } from '../utils';
* API.
*
* @throws service: {@link UpdateEndpointException} - Thrown when the underlying Pinpoint service returns an error.
* @throws validation: {@link AnalyticsValidationErrorCode} - Thrown when the provided parameters or library
* @throws validation: {@link AnalyticsValidationErrorCode} - Thrown when the provided parameters or library
* configuration is incorrect.
*
*
* @returns A promise that will resolve when the operation is complete.
*
*
* @example
* ```ts
* // Identify a user with Pinpoint
* await identifyUser({
* userId,
* userProfile: {
* attributes: {
* email: [userEmail],
* email: [userEmail]
* customProperties: {
* phoneNumber: ['555-555-5555'],
* },
* }
* });
* ```
*
*
* @example
* ```ts
* // Identify a user with Pinpoint with some additional demographics
* await identifyUser({
* userId,
* userProfile: {
* attributes: {
* email: [userEmail],
* email: [userEmail]
* customProperties: {
* phoneNumber: ['555-555-5555'],
* },
* demographic: {
* platform: 'ios',
Expand All @@ -54,15 +56,18 @@ import { resolveConfig, resolveCredentials } from '../utils';
export const identifyUser = async ({
userId,
userProfile,
options,
}: IdentifyUserInput): Promise<void> => {
const { credentials, identityId } = await resolveCredentials();
const { appId, region } = resolveConfig();
const { userAttributes } = options?.serviceOptions ?? {};
updateEndpoint({
appId,
category: 'Analytics',
credentials,
identityId,
region,
userAttributes,
userId,
userProfile,
userAgentValue: getAnalyticsUserAgentString(AnalyticsAction.UpdateEndpoint),
Expand Down
1 change: 1 addition & 0 deletions packages/analytics/src/providers/pinpoint/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

export { UpdateEndpointException } from './errors';
export { RecordInput, IdentifyUserInput } from './inputs';
export { IdentifyUserOptions } from './options';
18 changes: 3 additions & 15 deletions packages/analytics/src/providers/pinpoint/types/inputs.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { UserProfile } from '@aws-amplify/core';
import { PinpointAnalyticsEvent } from '@aws-amplify/core/internals/providers/pinpoint';
import { IdentifyUserOptions } from '.';
import { AnalyticsIdentifyUserInput } from '../../../types';

/**
* Input type for Pinpoint record API.
Expand All @@ -14,17 +15,4 @@ export type RecordInput = {
event: PinpointAnalyticsEvent;
};

/**
* Input type for Pinpoint identifyUser API.
*/
export type IdentifyUserInput = {
/**
* A User ID associated to the current device.
*/
userId: string;

/**
* Additional information about the user and their device.
*/
userProfile: UserProfile;
};
export type IdentifyUserInput = AnalyticsIdentifyUserInput<IdentifyUserOptions>;
12 changes: 12 additions & 0 deletions packages/analytics/src/providers/pinpoint/types/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { PinpointServiceOptions } from '@aws-amplify/core/internals/providers/pinpoint';

/**
* Options specific to Pinpoint identityUser.
*/
export type IdentifyUserOptions = Pick<
PinpointServiceOptions,
'userAttributes'
>;
2 changes: 1 addition & 1 deletion packages/analytics/src/types/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface EventMetrics {
[key: string]: number;
}

export interface pageViewTrackOpts {
export interface PageViewTrackOpts {
enable: boolean;
type?: string;
eventName?: string;
Expand Down
16 changes: 15 additions & 1 deletion packages/analytics/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

export * from './analytics';
export {
AutoTrackAttributes,
AutoTrackEventOpts,
AutoTrackPageViewOpts,
AutoTrackSessionOpts,
EventAttributes,
EventMetrics,
EventTrackOpts,
PageViewTrackOpts,
SessionTrackOpts,
} from './analytics';

export { AnalyticsServiceOptions } from './options';

export { AnalyticsIdentifyUserInput } from './inputs';
29 changes: 29 additions & 0 deletions packages/analytics/src/types/inputs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { UserProfile } from '@aws-amplify/core';
import { AnalyticsServiceOptions } from '.';

/**
* Constructs an `identifyUser` input.
*/
export type AnalyticsIdentifyUserInput<
ServiceOptions extends AnalyticsServiceOptions = AnalyticsServiceOptions
> = {
/**
* A User ID associated to the current device.
*/
userId: string;

/**
* Additional information about the user and their device.
*/
userProfile: UserProfile;

/**
* Options to be passed to the API.
*/
options?: {
serviceOptions?: ServiceOptions;
};
};
7 changes: 7 additions & 0 deletions packages/analytics/src/types/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

/**
* Base type for service options.
*/
export type AnalyticsServiceOptions = any;
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const getExpectedInput = ({
location,
metrics,
optOut,
userAttributes,
userId,
}: any) =>
expect.objectContaining({
Expand Down Expand Up @@ -50,7 +51,7 @@ export const getExpectedInput = ({
OptOut: optOut,
User: {
UserId: userId,
UserAttributes: attributes,
UserAttributes: userAttributes,
},
}),
});
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,12 @@ describe('Pinpoint Provider API: updateEndpoint', () => {
{ credentials, region },
getExpectedInput({
address,
attributes: userProfile.attributes,
attributes: {
email: [userProfile.email],
name: [userProfile.name],
plan: [userProfile.plan],
...userProfile.customProperties,
},
channelType,
demographic,
location,
Expand Down
5 changes: 4 additions & 1 deletion packages/core/__tests__/providers/pinpoint/testUtils/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ export const identityId = 'identity-id';
export const region = 'region';
export const userId = 'user-id';
export const userProfile = {
attributes: {
customProperties: {
hobbies: ['biking', 'climbing'],
},
email: 'email',
name: 'name',
plan: 'plan',
};
export const uuid = 'uuid';
22 changes: 19 additions & 3 deletions packages/core/src/providers/pinpoint/apis/updateEndpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,23 @@ export const updateEndpoint = async ({
identityId,
optOut,
region,
userAttributes,
userId,
userProfile,
userAgentValue,
}: PinpointUpdateEndpointInput): Promise<void> => {
const endpointId = await getEndpointId(appId, category);
// only generate a new endpoint id if one was not found in cache
const createdEndpointId = !endpointId ? uuidv4() : undefined;
const { attributes, demographic, location, metrics } = userProfile ?? {};
const {
customProperties,
demographic,
email,
location,
metrics,
name,
plan,
} = userProfile ?? {};
const clientInfo = ClientDevice.clientInfo();
const mergedDemographic = {
appVersion: clientInfo.appVersion,
Expand All @@ -39,6 +48,13 @@ export const updateEndpoint = async ({
platform: clientInfo.platform,
...demographic,
};
const shouldAddAttributes = email || customProperties || name || plan;
const attributes = {
...(email && { email: [email] }),
...(name && { name: [name] }),
...(plan && { plan: [plan] }),
...customProperties,
};
const input: UpdateEndpointInput = {
ApplicationId: appId,
EndpointId: endpointId ?? createdEndpointId,
Expand All @@ -47,7 +63,7 @@ export const updateEndpoint = async ({
EffectiveDate: new Date().toISOString(),
ChannelType: channelType,
Address: address,
Attributes: attributes,
Attributes: shouldAddAttributes ? attributes : undefined,
Demographic: {
AppVersion: mergedDemographic.appVersion,
Locale: mergedDemographic.locale,
Expand All @@ -70,7 +86,7 @@ export const updateEndpoint = async ({
OptOut: optOut,
User: {
UserId: userId ?? identityId,
UserAttributes: attributes,
UserAttributes: userAttributes,
},
},
};
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/providers/pinpoint/types/pinpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type PinpointProviderConfig = {
export type PinpointServiceOptions = {
address?: string;
optOut?: 'ALL' | 'NONE';
userAttributes?: Record<string, string[]>;
};

export type PinpointSession = {
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/types/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface AmplifyConfig {
}

export type UserProfile = {
attributes?: Record<string, string[]>;
customProperties?: Record<string, string[]>;
demographic?: {
appVersion?: string;
locale?: string;
Expand All @@ -27,6 +27,7 @@ export type UserProfile = {
platformVersion?: string;
timezone?: string;
};
email?: string;
location?: {
city?: string;
country?: string;
Expand All @@ -36,6 +37,8 @@ export type UserProfile = {
region?: string;
};
metrics?: Record<string, number>;
name?: string;
plan?: string;
};

/**
Expand Down

0 comments on commit f3dad3b

Please sign in to comment.