Skip to content

[FSSDK-8651] added support for user agent parser for odp #854

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

Merged
merged 4 commits into from
Aug 21, 2023
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
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"jest.rootPath": "/workspaces/javascript-sdk/packages/optimizely-sdk",
"jest.jestCommandLine": "./node_modules/.bin/jest",
"jest.autoRevealOutput": "on-exec-error"
"jest.autoRevealOutput": "on-exec-error",
"editor.tabSize": 2
}
35 changes: 34 additions & 1 deletion lib/core/odp/odp_event_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { OdpEvent } from './odp_event';
import { OdpConfig } from './odp_config';
import { IOdpEventApiManager } from './odp_event_api_manager';
import { invalidOdpDataFound } from './odp_utils';
import { IUserAgentParser } from './user_agent_parser';

const MAX_RETRIES = 3;

Expand Down Expand Up @@ -123,6 +124,19 @@ export abstract class OdpEventManager implements IOdpEventManager {
*/
private readonly clientVersion: string;

/**
* Version of the client being used
* @private
*/
private readonly userAgentParser?: IUserAgentParser;


/**
* Information about the user agent
* @private
*/
private readonly userAgentData?: Map<string, unknown>;

constructor({
odpConfig,
apiManager,
Expand All @@ -132,6 +146,7 @@ export abstract class OdpEventManager implements IOdpEventManager {
queueSize,
batchSize,
flushInterval,
userAgentParser,
}: {
odpConfig: OdpConfig;
apiManager: IOdpEventApiManager;
Expand All @@ -141,6 +156,7 @@ export abstract class OdpEventManager implements IOdpEventManager {
queueSize?: number;
batchSize?: number;
flushInterval?: number;
userAgentParser?: IUserAgentParser;
}) {
this.odpConfig = odpConfig;
this.apiManager = apiManager;
Expand All @@ -149,6 +165,22 @@ export abstract class OdpEventManager implements IOdpEventManager {
this.clientVersion = clientVersion;
this.initParams(batchSize, queueSize, flushInterval);
this.state = STATE.STOPPED;
this.userAgentParser = userAgentParser;

if (userAgentParser) {
const { os, device } = userAgentParser.parseUserAgentInfo();

const userAgentInfo: Record<string, unknown> = {
'os': os.name,
'os_version': os.version,
'device_type': device.type,
'model': device.model,
};

this.userAgentData = new Map<string, unknown>(
Object.entries(userAgentInfo).filter(([key, value]) => value != null && value != undefined)
);
}

this.apiManager.updateSettings(odpConfig);
}
Expand Down Expand Up @@ -408,7 +440,8 @@ export abstract class OdpEventManager implements IOdpEventManager {
* @private
*/
private augmentCommonData(sourceData: Map<string, unknown>): Map<string, unknown> {
const data = new Map<string, unknown>();
const data = new Map<string, unknown>(this.userAgentData);

data.set('idempotence_id', uuid());
data.set('data_source_type', 'sdk');
data.set('data_source', this.clientEngine);
Expand Down
26 changes: 26 additions & 0 deletions lib/core/odp/user_agent_info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright 2023, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export type UserAgentInfo = {
os: {
name?: string,
version?: string,
},
device: {
type?: string,
model?: string,
}
};
21 changes: 21 additions & 0 deletions lib/core/odp/user_agent_parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Copyright 2023, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { UserAgentInfo } from "./user_agent_info";

export interface IUserAgentParser {
Copy link
Contributor

Choose a reason for hiding this comment

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

Wondering if we care much about this parser. What about let them inject userAgentInfo as an object or make it more general as "clientOdpInfo" letting them add beyond the user-agent info?

Copy link
Contributor Author

@raju-opti raju-opti Aug 21, 2023

Choose a reason for hiding this comment

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

Yeah, we can rename the interface and add a general map<string, unknown> in the UserAgentInfo type.

Copy link
Contributor

Choose a reason for hiding this comment

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

A question is while we can make it flexible, we may want to pre-define a few "required" names like os-name, os-version, device-model, ... to be consistent with other SDKs. @raju-opti

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah. UserAgentInfo type already has os and device fields. We can add a few more.

parseUserAgentInfo(): UserAgentInfo,
}
51 changes: 51 additions & 0 deletions lib/index.browser.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ if (!global.window) {
}
}

const pause = (timeoutMilliseconds) => {
return new Promise(resolve => setTimeout(resolve, timeoutMilliseconds));
};

describe('javascript-sdk (Browser)', function() {
var clock;
beforeEach(function() {
Expand Down Expand Up @@ -838,6 +842,53 @@ describe('javascript-sdk (Browser)', function() {
sinon.assert.called(fakeEventManager.sendEvent);
});

it('should augment odp events with user agent data if userAgentParser is provided', async () => {
const userAgentParser = {
parseUserAgentInfo() {
return {
os: { 'name': 'windows', 'version': '11' },
device: { 'type': 'laptop', 'model': 'thinkpad' },
}
}
}

const fakeRequestHandler = {
makeRequest: sinon.spy(function (requestUrl, headers, method, data) {
return {
abort: () => {},
responsePromise: Promise.resolve({ statusCode: 200 }),
}
})
};

const client = optimizelyFactory.createInstance({
datafile: testData.getOdpIntegratedConfigWithSegments(),
errorHandler: fakeErrorHandler,
eventDispatcher: fakeEventDispatcher,
eventBatchSize: null,
logger,
odpOptions: {
userAgentParser,
eventRequestHandler: fakeRequestHandler,
},
});
const readyData = await client.onReady();

assert.equal(readyData.success, true);
assert.isUndefined(readyData.reason);

client.sendOdpEvent('test', '', new Map([['eamil', '[email protected]']]), new Map([['key', 'value']]));
clock.tick(10000);

const eventRequestUrl = new URL(fakeRequestHandler.makeRequest.lastCall.args[0]);
const searchParams = eventRequestUrl.searchParams;

assert.equal(searchParams.get('os'), 'windows');
assert.equal(searchParams.get('os_version'), '11');
assert.equal(searchParams.get('device_type'), 'laptop');
assert.equal(searchParams.get('model'), 'thinkpad');
});

it('should convert fs-user-id, FS-USER-ID, and FS_USER_ID to fs_user_id identifier when calling sendOdpEvent', async () => {
const fakeEventManager = {
updateSettings: sinon.spy(),
Expand Down
6 changes: 6 additions & 0 deletions lib/index.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import { OptimizelyDecideOption, Client, Config, OptimizelyOptions } from './sha
import { createHttpPollingDatafileManager } from './plugins/datafile_manager/browser_http_polling_datafile_manager';
import { BrowserOdpManager } from './plugins/odp_manager/index.browser';
import Optimizely from './optimizely';
import { IUserAgentParser } from './core/odp/user_agent_parser';
import { getUserAgentParser } from './plugins/odp/user_agent_parser/index.browser';

const logger = getLogger();
logHelper.setLogHandler(loggerPlugin.createLogger());
Expand Down Expand Up @@ -164,6 +166,7 @@ const __internalResetRetryState = function(): void {

const setLogHandler = logHelper.setLogHandler;
const setLogLevel = logHelper.setLogLevel;

export {
loggerPlugin as logging,
defaultErrorHandler as errorHandler,
Expand All @@ -174,6 +177,8 @@ export {
createInstance,
__internalResetRetryState,
OptimizelyDecideOption,
IUserAgentParser,
getUserAgentParser,
};

export default {
Expand All @@ -186,6 +191,7 @@ export default {
createInstance,
__internalResetRetryState,
OptimizelyDecideOption,
getUserAgentParser,
};

export * from './export_types';
33 changes: 33 additions & 0 deletions lib/plugins/odp/user_agent_parser/index.browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright 2023, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { UAParser } from 'ua-parser-js';
import { UserAgentInfo } from "../../../core/odp/user_agent_info";
import { IUserAgentParser } from '../../../core/odp/user_agent_parser';

const userAgentParser: IUserAgentParser = {
parseUserAgentInfo(): UserAgentInfo {
const parser = new UAParser();
const agentInfo = parser.getResult();
const { os, device } = agentInfo;
return { os, device };
}
}

export function getUserAgentParser(): IUserAgentParser {
return userAgentParser;
}

1 change: 1 addition & 0 deletions lib/plugins/odp_manager/index.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export class BrowserOdpManager extends OdpManager {
flushInterval: odpOptions?.eventFlushInterval,
batchSize: odpOptions?.eventBatchSize,
queueSize: odpOptions?.eventQueueSize,
userAgentParser: odpOptions?.userAgentParser,
});
}

Expand Down
2 changes: 2 additions & 0 deletions lib/shared_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { IOdpSegmentManager } from './core/odp/odp_segment_manager';
import { IOdpEventApiManager } from './core/odp/odp_event_api_manager';
import { IOdpEventManager } from './core/odp/odp_event_manager';
import { IOdpManager } from './core/odp/odp_manager';
import { IUserAgentParser } from './core/odp/user_agent_parser';

export interface BucketerParams {
experimentId: string;
Expand Down Expand Up @@ -105,6 +106,7 @@ export interface OdpOptions {
eventApiTimeout?: number;
eventRequestHandler?: RequestHandler;
eventManager?: IOdpEventManager;
userAgentParser?: IUserAgentParser;
}

export interface ListenerPayload {
Expand Down
19 changes: 15 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"decompress-response": "^4.2.1",
"json-schema": "^0.4.0",
"murmurhash": "^2.0.1",
"ua-parser-js": "^1.0.35",
"uuid": "^8.3.2"
},
"devDependencies": {
Expand All @@ -57,6 +58,7 @@
"@types/mocha": "^5.2.7",
"@types/nise": "^1.4.0",
"@types/node": "^18.7.18",
"@types/ua-parser-js": "^0.7.36",
"@types/uuid": "^3.4.4",
"@typescript-eslint/eslint-plugin": "^5.33.0",
"@typescript-eslint/parser": "^5.33.0",
Expand Down
37 changes: 37 additions & 0 deletions tests/odpEventManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import { anything, capture, instance, mock, resetCalls, spy, verify, when } from
import { IOdpEventApiManager } from '../lib/core/odp/odp_event_api_manager';
import { LogHandler, LogLevel } from '../lib/modules/logging';
import { OdpEvent } from '../lib/core/odp/odp_event';
import { IUserAgentParser } from '../lib/core/odp/user_agent_parser';
import { UserAgentInfo } from '../lib/core/odp/user_agent_info';

const API_KEY = 'test-api-key';
const API_HOST = 'https://odp.example.com';
Expand Down Expand Up @@ -372,6 +374,41 @@ describe('OdpEventManager', () => {
expect(events[1].data.size).toEqual(PROCESSED_EVENTS[1].data.size);
});

it('should augment events with data from user agent parser', async () => {
const userAgentParser : IUserAgentParser = {
parseUserAgentInfo: function (): UserAgentInfo {
return {
os: { 'name': 'windows', 'version': '11' },
device: { 'type': 'laptop', 'model': 'thinkpad' },
}
}
}

const eventManager = new OdpEventManager({
odpConfig,
apiManager,
logger,
clientEngine,
clientVersion,
batchSize: 10,
flushInterval: 100,
userAgentParser,
});

eventManager.start();
EVENTS.forEach(event => eventManager.sendEvent(event));
await pause(1000);

verify(mockApiManager.sendEvents(anything())).called();
const [events] = capture(mockApiManager.sendEvents).last();
const event = events[0];

expect(event.data.get('os')).toEqual('windows');
expect(event.data.get('os_version')).toEqual('11');
expect(event.data.get('device_type')).toEqual('laptop');
expect(event.data.get('model')).toEqual('thinkpad');
});

it('should retry failed events', async () => {
// all events should fail ie shouldRetry = true
when(mockApiManager.sendEvents(anything())).thenResolve(true);
Expand Down
Loading