-
Notifications
You must be signed in to change notification settings - Fork 83
[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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
} |
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, | ||
} | ||
}; |
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
raju-opti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() { | ||
|
@@ -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(), | ||
|
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 { | ||
raju-opti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const parser = new UAParser(); | ||
const agentInfo = parser.getResult(); | ||
const { os, device } = agentInfo; | ||
return { os, device }; | ||
} | ||
} | ||
|
||
export function getUserAgentParser(): IUserAgentParser { | ||
return userAgentParser; | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.