Skip to content

Instantly share code, notes, and snippets.

@asaschachar
Last active May 15, 2020 04:58
Show Gist options
  • Save asaschachar/27fa51a94b3ec077c253e7a231012771 to your computer and use it in GitHub Desktop.
Save asaschachar/27fa51a94b3ec077c253e7a231012771 to your computer and use it in GitHub Desktop.
ReactABTests
/* Complete code example of App.js by the end of the video */
/* NOTE: Replace <Your_SDK_Key> with the SDK Key of your project below */
import React from 'react';
import logo from './logo.svg';
import './App.css';
import {
createInstance,
OptimizelyFeature,
OptimizelyProvider,
withOptimizely
} from '@optimizely/react-sdk';
const optimizely = createInstance({
sdkKey: '<Your SDK Key>'
});
class PurchaseButton extends React.Component {
onClick = () => {
const { optimizely } = this.props
// after we’ve confirmed purchase completed
optimizely.track('purchase')
}
render() {
return (
<button onClick={this.onClick}>
Purchase
</button>
)
}
}
const WrappedPurchaseButton = withOptimizely(PurchaseButton)
function App() {
return (
<OptimizelyProvider
optimizely={optimizely}
user={{
id: "user983"
}}
>
<div className="App">
<header className="App-header">
<OptimizelyFeature feature="discount">
{(enabled, variables) => `Got a discount of $${variables.amount}`}
</OptimizelyFeature>
<WrappedPurchaseButton></WrappedPurchaseButton>
</header>
</div>
</OptimizelyProvider>
);
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment