Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 2e7b9b2

Browse files
authored
2.0 consolidation (optimizely#77)
* Consolidate all the Node+JS code into this repo. * Run tests with Karma locally. * Include the dist files for real. * Include lib files and also lib target for node modules. * Use scoped package name. * Fix browser field reference. * Update documentation in the repo. * change umd file name reference in README * change travis file and request node module (optimizely#73) * change travis file and request node module * remove previous keys * add Key info in README * requirement changes * Automatically parse datafile when it is JSON (optimizely#75) * Fix whitespace (optimizely#76) * Update package name to optimizely-sdk-core. * Fix tests and change package name to core. * Make sure to run ALL tests. * Remove min node bundle because of compilation errors.
1 parent f429d51 commit 2e7b9b2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+12283
-351
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
.DS_STORE
22
npm-debug.log
3-
node_modules
43
dist
54
node_modules
65

.travis.yml

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
language: node_js
22
node_js:
3-
- "5"
4-
- "6"
5-
- "node"
3+
- '4'
4+
- '6'
5+
- '8'
6+
- node
67
branches:
78
only:
8-
- master
9+
- master
10+
- 2.0-consolidation
911
env:
1012
global:
11-
# BROWSER_STACK_USERNAME
12-
- secure: "OyWUgwuPAxKaf8X627DjZDkp+h5uvLntJYcMTtq4FUTaokKBk7UbNs5MW2pP1rbzHIqEQDuj3WneuODMYpumH/gq9wwFSNgZipHzSbKUWMOPW3njIuN6N9ikkzVijEUh2ljK4wC3BVOWfaatR4J/KTpQTaJ6LanOabCo2StB70bGxfFvrHAAfRAAAGjVyAPovCcuDI4glfdEQa0H95iTKxCpPK9/oDDZCC8pyjHhb7zVin/D2JDPSVlEmiUPanTP3L//ICLKcdL/v3W5b4Sh1nuKFsHMxEXAPTl7/oTkG7hAcIILBmKhQz58P/rgmjSkz5GvC4CbwSOJigQAY7AUTZuW0S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eyiBIoejWiES2zk4U2BVQaHWRO3vR90En7M9f7DoCzDLklOg2ZvSeLQ8XmX/979iIdGMUmVGW5WffYj+O2piWnaPVPeS5VsUPfZFoFfnIDHkTAtI5eDxkpuyKDqJpQr+fM5K4w9A9G0wBDDOIoBVpqjUo3OK7PyUBRyhA3AesK/4E0iWtmIEK3IQRyzVrL9ifwmTA0ikrHIBETslcYF78qUOVXwn+VYyZhJHlakG6SOGWwOl92cZcEf9wGRP8hFgB5f4kZITgNQMRzj/x/K+YPLG4+8V9DBKXYrd6uUaItJ78pE6zdMS3BCduds98FqfsYNn7kfa1CRXtJ3M/SC6ZAFjE/8lhEcg=
1517
before_script:
16-
- npm install grunt-cli -g
17-
install: "npm install"
18+
- npm install grunt-cli -g
19+
install: npm install
1820
script:
19-
- "npm run test-travis"
21+
- npm run test-travis
2022
after_success:
2123
- npm run coveralls

CHANGELOG

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
-------------------------------------------------------------------------------
2+
2.0.0-beta
3+
-------------------------------------------------------------------------------
4+
* Consolidate JS and Node repos and packages into one.
5+
-------------------------------------------------------------------------------
6+
17
-------------------------------------------------------------------------------
28
1.6.0
39
-------------------------------------------------------------------------------

CONTRIBUTING.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ We welcome contributions and feedback! All contributors must sign our [Contribut
77
1. Create a branch off of `master`: `git checkout -b YOUR_NAME/branch_name`.
88
2. Commit your changes. Make sure to add tests!
99
3. Run `npm run lint` to ensure there are no lint errors.
10-
4. Run `webpack` to generate the built and minified file for those not installing via `npm`
11-
5. `git push` your changes to GitHub.
12-
6. Make sure that all unit tests are passing and that there are no merge conflicts between your branch and `master`.
13-
7. Open a pull request from `YOUR_NAME/branch_name` to `master`.
14-
8. A repository maintainer will review your pull request and, if all goes well, squash and merge it!
10+
4. `git push` your changes to GitHub.
11+
5. Make sure that all unit tests are passing and that there are no merge conflicts between your branch and `master`.
12+
6. Open a pull request from `YOUR_NAME/branch_name` to `master`.
13+
7. A repository maintainer will review your pull request and, if all goes well, squash and merge it!
1514

1615
## Pull request acceptance criteria
1716

README.md

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,28 @@ This repository houses the JavaScript SDK for Optimizely X Full Stack.
66

77
### Installing the SDK
88

9-
The SDK is available through [npm](https://npmjs.com/package/optimizely-client-sdk). To install:
9+
The SDK is available through [npm](https://npmjs.com/package/optimizely-sdk-core). To install:
1010

1111
```
12-
npm install optimizely-client-sdk --save
12+
npm install @optimizely/optimizely-sdk-core --save
1313
```
1414

1515
Or to use in a non CommonJS fashion:
1616

1717
1. Run `npm run build`
18-
2. Pull in `dist/optimizely.min.js` as a `<script>`
18+
2. Pull in `dist/optimizely.browser.umd.min.js` as a `<script>`
1919
3. Use as global variable `window.optimizelyClient`
2020

21+
### Migrating from 1.x.x
22+
23+
This version represents a major version change and, as such, introduces some breaking changes:
24+
25+
- We have changed the package name to more accurately reflect the implementation of the SDK. Instead of using `optimizely-sdk`, we have migrated it to `optimizely-sdk-core` as this contains the core functionality of the SDK and leaves out things like datafile management. Instead the new `optimizely-sdk` package will be a wrapper around `optimizely-sdk-core` that will include more advanced functionality such as datafile management and event dispatch retries and can be used straight out of the box with minimal config. More on this later!
26+
27+
- The Node SDK is now combined with the JavaScript SDK so that we have one `optimizely-sdk-core` package that works across both server + browser environments.
28+
29+
- You will no longer be able to pass in `revenue` value as a stand-alone argument to the `track` call. Instead you will need to pass it as an entry in the [`eventTags`](https://developers.optimizely.com/x/solutions/sdks/reference/index.html?language=javascript#event-tags).
30+
2131
### Using the SDK
2232
See the Optimizely X Full Stack testing [developer documentation](http://developers.optimizely.com/server/reference/index.html) to learn how to set up your first JavaScript project and use the SDK.
2333

@@ -34,6 +44,28 @@ You can run all unit tests with:
3444
npm test
3545
```
3646

47+
### Build distribution packages
48+
49+
```
50+
npm run build
51+
```
52+
53+
This command will build several distribution bundles under the `dist` directory:
54+
1. optimizely.browser.cjs.js - This is the main entry point for browser/client-side bundles
55+
2. optimizely.browser.umd.js - This is used when not packaging the optimizely-sdk with your own JS bundles. Instead you would load this script as a `<script>` tag and reference it via the global var `optimizelyClient`
56+
3. optimizely.node.js - This is the main entry point for Node apps
57+
58+
The browser bundles also come with a minified / production-ready version.
59+
60+
### Environment Variables
61+
62+
The .yml of this project contains environment vairables for ```BROWSER_STACK_USERNAME``` and ```BROWSER_STACK_ACCESS_KEY```.
63+
64+
These variables, created in BrowserStack, are encrypted by the TravisCI public key. This is done directly with the TravisCI command line tools; for additional information see travis encrypt-file.
65+
3766
### Contributing
3867

3968
Please see [CONTRIBUTING](CONTRIBUTING.md).
69+
70+
71+

karma.bs.conf.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ module.exports = function(config) {
88
basePath: '',
99

1010
//plugins
11-
plugins: ['karma-mocha', 'karma-webpack', 'karma-browserstack-launcher'],
11+
plugins: ['karma-mocha', 'karma-webpack', require('karma-browserstack-launcher')],
1212

1313
//browserStack setup
1414
browserStack: {
15-
username: 'echo $BROWSER_STACK_USERNAME',
16-
accessKey: 'echo $BROWSER_STACK_ACCESS_KEY'
15+
username: process.env.BROWSER_STACK_USERNAME,
16+
accessKey: process.env.BROWSER_STACK_ACCESS_KEY
1717
},
1818

1919
// to avoid DISCONNECTED messages when connecting to BrowserStack
@@ -86,8 +86,7 @@ module.exports = function(config) {
8686

8787
// list of files / patterns to load in the browser
8888
files: [
89-
{pattern: './tests.js', watched: false},
90-
{pattern: './lib/**/*tests.js', watched: false}
89+
'./lib/index.browser.tests.js'
9190
],
9291

9392

@@ -99,7 +98,6 @@ module.exports = function(config) {
9998
// preprocess matching files before serving them to the browser
10099
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
101100
preprocessors: {
102-
'./tests.js': ['webpack'],
103101
'./lib/**/*tests.js': ['webpack']
104102
},
105103

karma.conf.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ module.exports = function(config) {
1515

1616
// list of files / patterns to load in the browser
1717
files: [
18-
'./tests.js',
19-
'./lib/**/*tests.js'
18+
'./lib/index.browser.tests.js'
2019
],
2120

2221

@@ -68,15 +67,14 @@ module.exports = function(config) {
6867
concurrency: Infinity,
6968

7069
preprocessors: {
71-
// add webpack as preprocessor
72-
'./tests.js': ['webpack'],
73-
'./lib/**/*tests.js': ['webpack']
74-
},
70+
// add webpack as preprocessor
71+
'./lib/**/*tests.js': ['webpack']
72+
},
7573

7674
plugins: [
7775
require("karma-webpack"),
7876
'karma-mocha',
79-
'karma-chai',
77+
require('karma-chai'),
8078
'karma-chrome-launcher'
8179
]
8280
})

lib/core/audience_evaluator/index.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright 2016, Optimizely
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
var conditionEvaluator = require('../condition_evaluator');
17+
18+
module.exports = {
19+
/**
20+
* Determine if the given user attributes satisfy the given audience conditions
21+
* @param {Object[]} audiences Audiences to match the user attributes against
22+
* @param {Object[]} audiences.conditions Audience conditions to match the user attributes against
23+
* @param {Object} userAttributes Hash representing user attributes which will be used in determining if
24+
* the audience conditions are met
25+
* @return {Boolean} True if the user attributes match the given audience conditions
26+
*/
27+
evaluate: function(audiences, userAttributes) {
28+
// if there are no audiences, return true because that means ALL users are included in the experiment
29+
if (!audiences || audiences.length === 0) {
30+
return true;
31+
}
32+
33+
// if no user attributes specified, return false
34+
if (!userAttributes) {
35+
return false;
36+
}
37+
38+
for (var i = 0; i < audiences.length; i++) {
39+
var audience = audiences[i];
40+
var conditions = audience.conditions;
41+
if (conditionEvaluator.evaluate(conditions, userAttributes)) {
42+
return true;
43+
}
44+
}
45+
46+
return false;
47+
},
48+
};
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* Copyright 2016, Optimizely
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
var audienceEvaluator = require('./');
17+
var chai = require('chai');
18+
var assert = chai.assert;
19+
20+
var chromeUserAudience = {
21+
conditions: ['and', {'name': 'browser_type', 'value': 'chrome'}],
22+
};
23+
var iphoneUserAudience = {
24+
conditions: ['and', {'name': 'device_model', 'value': 'iphone'}],
25+
};
26+
27+
describe('lib/core/audience_evaluator', function() {
28+
describe('APIs', function() {
29+
describe('evaluate', function() {
30+
it('should return true if there are no audiences', function() {
31+
assert.isTrue(audienceEvaluator.evaluate([], {}));
32+
});
33+
34+
it('should return false if there are audiences but no attributes', function() {
35+
assert.isFalse(audienceEvaluator.evaluate([chromeUserAudience], {}));
36+
});
37+
38+
it('should return true if any of the audience conditions are met', function() {
39+
var iphoneUsers = {
40+
'device_model': 'iphone',
41+
};
42+
43+
var chromeUsers = {
44+
'browser_type': 'chrome',
45+
};
46+
47+
var iphoneChromeUsers = {
48+
'browser_type': 'chrome',
49+
'device_model': 'iphone',
50+
};
51+
52+
assert.isTrue(audienceEvaluator.evaluate([chromeUserAudience, iphoneUserAudience], iphoneUsers));
53+
assert.isTrue(audienceEvaluator.evaluate([chromeUserAudience, iphoneUserAudience], chromeUsers));
54+
assert.isTrue(audienceEvaluator.evaluate([chromeUserAudience, iphoneUserAudience], iphoneChromeUsers));
55+
});
56+
57+
it('should return false if none of the audience conditions are met', function() {
58+
var nexusUsers = {
59+
'device_model': 'nexus5',
60+
};
61+
62+
var safariUsers = {
63+
'browser_type': 'safari',
64+
};
65+
66+
var nexusSafariUsers = {
67+
'browser_type': 'safari',
68+
'device_model': 'nexus5',
69+
};
70+
71+
assert.isFalse(audienceEvaluator.evaluate([chromeUserAudience, iphoneUserAudience], nexusUsers));
72+
assert.isFalse(audienceEvaluator.evaluate([chromeUserAudience, iphoneUserAudience], safariUsers));
73+
assert.isFalse(audienceEvaluator.evaluate([chromeUserAudience, iphoneUserAudience], nexusSafariUsers));
74+
});
75+
});
76+
});
77+
});

0 commit comments

Comments
 (0)