Skip to content

Commit e9e948f

Browse files
authored
docs: experiment examples - react (#48)
1 parent 7da2849 commit e9e948f

34 files changed

+18392
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Getting Started with Create React App
2+
3+
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
4+
5+
## Available Scripts
6+
7+
In the project directory, you can run:
8+
9+
### `npm start`
10+
11+
Runs the app in the development mode.\
12+
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
13+
14+
The page will reload if you make edits.\
15+
You will also see any lint errors in the console.
16+
17+
### `npm run build`
18+
19+
Builds the app for production to the `build` folder.\
20+
It correctly bundles React in production mode and optimizes the build for the best performance.
21+
22+
The build is minified and the filenames include the hashes.\
23+
Your app is ready to be deployed!
24+
25+
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
26+
27+
### `npm run eject`
28+
29+
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
30+
31+
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
32+
33+
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
34+
35+
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
36+
37+
## Learn More
38+
39+
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
40+
41+
To learn React, check out the [React documentation](https://reactjs.org/).
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "react-app",
3+
"version": "0.1.0",
4+
"private": true,
5+
"dependencies": {
6+
"@amplitude/analytics-browser": "^1.5.4",
7+
"@amplitude/experiment-js-client": "^1.5.6",
8+
"@types/node": "^16.11.29",
9+
"@types/react": "^18.0.7",
10+
"@types/react-dom": "^18.0.0",
11+
"react": "^18.0.0",
12+
"react-dom": "^18.0.0",
13+
"react-scripts": "5.0.1",
14+
"typescript": "^4.6.3"
15+
},
16+
"scripts": {
17+
"start": "react-scripts start",
18+
"build": "react-scripts build",
19+
"eject": "react-scripts eject"
20+
},
21+
"eslintConfig": {
22+
"extends": [
23+
"react-app"
24+
]
25+
},
26+
"browserslist": {
27+
"production": [
28+
">0.2%",
29+
"not dead",
30+
"not op_mini all"
31+
],
32+
"development": [
33+
"last 1 chrome version",
34+
"last 1 firefox version",
35+
"last 1 safari version"
36+
]
37+
}
38+
}
Binary file not shown.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
<meta name="theme-color" content="#000000" />
8+
<meta
9+
name="description"
10+
content="Web site created using create-react-app"
11+
/>
12+
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
13+
<!--
14+
manifest.json provides metadata used when your web app is installed on a
15+
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
16+
-->
17+
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
18+
<!--
19+
Notice the use of %PUBLIC_URL% in the tags above.
20+
It will be replaced with the URL of the `public` folder during the build.
21+
Only files inside the `public` folder can be referenced from the HTML.
22+
23+
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
24+
work correctly both with client-side routing and a non-root public URL.
25+
Learn how to configure a non-root public URL by running `npm run build`.
26+
-->
27+
<title>React App</title>
28+
</head>
29+
<body>
30+
<noscript>You need to enable JavaScript to run this app.</noscript>
31+
<div id="root"></div>
32+
<!--
33+
This HTML file is a template.
34+
If you open it directly in the browser, you will see an empty page.
35+
36+
You can add webfonts, meta tags, or analytics to this file.
37+
The build step will place the bundled scripts into the <body> tag.
38+
39+
To begin the development, run `npm start` or `yarn start`.
40+
To create a production bundle, use `npm run build` or `yarn build`.
41+
-->
42+
</body>
43+
</html>
Loading
Loading
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"short_name": "React App",
3+
"name": "Create React App Sample",
4+
"icons": [
5+
{
6+
"src": "favicon.ico",
7+
"sizes": "64x64 32x32 24x24 16x16",
8+
"type": "image/x-icon"
9+
},
10+
{
11+
"src": "logo192.png",
12+
"type": "image/png",
13+
"sizes": "192x192"
14+
},
15+
{
16+
"src": "logo512.png",
17+
"type": "image/png",
18+
"sizes": "512x512"
19+
}
20+
],
21+
"start_url": ".",
22+
"display": "standalone",
23+
"theme_color": "#000000",
24+
"background_color": "#ffffff"
25+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# https://www.robotstxt.org/robotstxt.html
2+
User-agent: *
3+
Disallow:
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
.App {
2+
text-align: center;
3+
wrap-option: break-word;
4+
}
5+
6+
.App-logo {
7+
height: 20vmin;
8+
pointer-events: none;
9+
}
10+
11+
@media (prefers-reduced-motion: no-preference) {
12+
.App-logo {
13+
animation: App-logo-spin infinite 20s linear;
14+
}
15+
}
16+
17+
.App-header {
18+
background-color: #282c34;
19+
min-height: 100vh;
20+
display: flex;
21+
flex-direction: column;
22+
align-items: center;
23+
justify-content: center;
24+
font-size: calc(10px + 1vmin);
25+
color: white;
26+
wrap-option: anywhere;
27+
}
28+
29+
.App-link {
30+
color: #61dafb;
31+
}
32+
33+
@keyframes App-logo-spin {
34+
from {
35+
transform: rotate(0deg);
36+
}
37+
to {
38+
transform: rotate(360deg);
39+
}
40+
}
41+
42+
.output {
43+
font-family: monospace, serif;
44+
font-size: calc(10px + 0.75vmin);
45+
wrap-option: break-word;
46+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import React, { useState } from "react";
2+
import logo from './logo.svg';
3+
import './App.css';
4+
import { experiment } from './index';
5+
6+
function App() {
7+
8+
const [output, setOutput] = useState('');
9+
10+
return (
11+
<div className="App">
12+
<header className="App-header">
13+
<img src={logo} className="App-logo" alt="logo" />
14+
<h2>Amplitude Analytics Browser Example with React</h2>
15+
<p>
16+
Click "Fetch" to fetch variants, then "Variant" or "All" to access variants from the SDK.
17+
<br />
18+
Open the console to view debug output from the SDK.
19+
</p>
20+
21+
<button onClick={() => {
22+
void experiment.fetch();
23+
}}>Fetch</button>
24+
25+
<button onClick={() => {
26+
// TODO Replace FLAG_KEY with your flag key
27+
const variant = experiment.variant('FLAG_KEY');
28+
setOutput(JSON.stringify(variant));
29+
}}>Variant</button>
30+
31+
<button onClick={() => {
32+
const variants = experiment.all();
33+
setOutput(JSON.stringify(variants));
34+
}}>All</button>
35+
36+
<div className="output">
37+
{output}
38+
</div>
39+
</header>
40+
</div>
41+
);
42+
}
43+
44+
export default App;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
body {
2+
margin: 0;
3+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
4+
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
5+
sans-serif;
6+
-webkit-font-smoothing: antialiased;
7+
-moz-osx-font-smoothing: grayscale;
8+
}
9+
10+
code {
11+
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
12+
monospace;
13+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom/client';
3+
import './index.css';
4+
import App from './App';
5+
import * as amplitude from '@amplitude/analytics-browser';
6+
import { Experiment } from '@amplitude/experiment-js-client';
7+
8+
/**
9+
* Initialize the Amplitude Analytics SDK.
10+
*/
11+
amplitude.init('a6dd847b9d2f03c816d4f3f8458cdc1d', '[email protected]');
12+
amplitude.identify(new amplitude.Identify().set('premium', true))
13+
14+
/**
15+
* Initialize the Amplitude Experiment SDK with the Amplitude Analytics
16+
* integration and export the initialized client.
17+
*
18+
* The user identity and user properties set in the analytics SDK will
19+
* automatically be used by the Experiment SDK on fetch().
20+
*/
21+
export const experiment = Experiment.initializeWithAmplitudeAnalytics(
22+
'client-QQEu7NCkqMmhdGdWl3Y4post5mZaVkCL',
23+
{ debug: true }
24+
);
25+
26+
const root = ReactDOM.createRoot(
27+
document.getElementById('root') as HTMLElement
28+
);
29+
root.render(
30+
<React.StrictMode>
31+
<App />
32+
</React.StrictMode>
33+
);
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/// <reference types="react-scripts" />
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"lib": [
5+
"dom",
6+
"dom.iterable",
7+
"esnext"
8+
],
9+
"allowJs": true,
10+
"skipLibCheck": true,
11+
"esModuleInterop": true,
12+
"allowSyntheticDefaultImports": true,
13+
"strict": true,
14+
"forceConsistentCasingInFileNames": true,
15+
"noFallthroughCasesInSwitch": true,
16+
"module": "esnext",
17+
"moduleResolution": "node",
18+
"resolveJsonModule": true,
19+
"isolatedModules": true,
20+
"noEmit": true,
21+
"jsx": "react-jsx"
22+
},
23+
"include": [
24+
"src"
25+
]
26+
}

0 commit comments

Comments
 (0)