Skip to content

Commit

Permalink
AMP-53883 add ampli js v2 example (#119)
Browse files Browse the repository at this point in the history
* AMP-53883 add ampli js v2 example

* Update browser/javascript/v2/react-app/README.md

Co-authored-by: Justin Fiedler <[email protected]>

* Update browser/javascript/v2/react-app/README.md

Co-authored-by: Justin Fiedler <[email protected]>

* Update browser/javascript/v2/react-app/src/App.js

Co-authored-by: Justin Fiedler <[email protected]>

* Replace logEvent with track

* Simplify code

* Update js example

* Update example

* Update example

* Update example

* Update example

* Fix test

* Update example

Co-authored-by: Justin Fiedler <[email protected]>
  • Loading branch information
dnshi and justin-fiedler authored Jul 1, 2022
1 parent 8004d71 commit 230d6cf
Show file tree
Hide file tree
Showing 23 changed files with 13,555 additions and 0 deletions.
2 changes: 2 additions & 0 deletions browser/javascript/v2/react-app/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
REACT_APP_AMPLITUDE_API_KEY=your-amplitide-api-key
REACT_APP_SEGMENT_WRITE_KEY=your-segment-write-key
23 changes: 23 additions & 0 deletions browser/javascript/v2/react-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
6 changes: 6 additions & 0 deletions browser/javascript/v2/react-app/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"printWidth": 120,
"proseWrap": "always",
"singleQuote": true,
"trailingComma": "all"
}
38 changes: 38 additions & 0 deletions browser/javascript/v2/react-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Ampli Browser SDK (2.0)

An example React app using the Ampli Browser SDK V2. Based on create-react-app.

# Usage

### Setup the project

You will need to do the following before running the app.

1. Create a `.env` with your API keys
1. `cp .env.example .env`
2. Set your Amplitude API key
3. (Optional) To use Segment plugin set a Segment write key
2. `yarn install`

### Run the app

You can run the app using the events from our sample tracking plan. This will log events to your Amplitude project.

- `yarn start`

### Pull your Ampli SDK

If you want to use your own tracking plan and Ampli SDK

1. `npm i -g @amplitude/ampli`
2. `ampli pull`
3. Update `app.js` to use the events from your tracking plan

# Project structure

- README.md - you are here \*
- - src/
- [App.js](src/App.js) - Example user app using Ampli SDK. A good place to start.
- ampli/
- [index.js](src/ampli/index.js) - Generated SDK, don't modify by hand. Update with `ampli pull`
- [schema.json](src/ampli/schema.json) - The full event schema for the tracking plan
11 changes: 11 additions & 0 deletions browser/javascript/v2/react-app/ampli.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Zone": "us",
"OrgId": "132559",
"WorkspaceId": "77b37977-cb3a-42eb-bce3-09f5f7c3adb7",
"SourceId": "6b939949-1003-4cf8-92a3-4bea7bf4e979",
"Runtime": "browser:javascript-ampli-v2",
"Branch": "main",
"Version": "0.0.0",
"VersionId": "79154a50-f057-4db5-9755-775e4e9f05e6",
"Path": "./src/ampli"
}
39 changes: 39 additions & 0 deletions browser/javascript/v2/react-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "react-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"@amplitude/analytics-browser": "^1.0.0",
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3",
"web-vitals": "^1.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --watchAll=false",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Binary file not shown.
43 changes: 43 additions & 0 deletions browser/javascript/v2/react-app/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions browser/javascript/v2/react-app/public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
3 changes: 3 additions & 0 deletions browser/javascript/v2/react-app/public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
38 changes: 38 additions & 0 deletions browser/javascript/v2/react-app/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
.App {
text-align: center;
}

.App-logo {
height: 40vmin;
pointer-events: none;
}

@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}

.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}

.App-link {
color: #61dafb;
}

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
Loading

0 comments on commit 230d6cf

Please sign in to comment.