├── .gitignore ├── INTERFACES.md ├── LINKS.md ├── README.md ├── package.json ├── public ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt ├── src ├── App.css ├── App.test.tsx ├── App.tsx ├── UIWithBad.tsx ├── UIWithEventMap.tsx ├── UIWithFuncs.tsx ├── UIWithOverloads.tsx ├── UIWithOverloadsAndEnums.tsx ├── analytics-bad.ts ├── analytics-eventmap.ts ├── analytics-funcs.ts ├── analytics-overloads-enums.ts ├── analytics-overloads.ts ├── index.css ├── index.tsx ├── logo.svg ├── react-app-env.d.ts ├── reportWebVitals.ts └── setupTests.ts ├── tsconfig.json └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 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* 24 | -------------------------------------------------------------------------------- /INTERFACES.md: -------------------------------------------------------------------------------- 1 | ```ts 2 | interface CheckoutEventProps { 3 | products: string[]; 4 | } 5 | interface AddToCartEventProps { 6 | product: string; 7 | quantity: number; 8 | } 9 | interface ViewProductEventProps { 10 | product: string; 11 | referrer: string; 12 | } 13 | ``` 14 | -------------------------------------------------------------------------------- /LINKS.md: -------------------------------------------------------------------------------- 1 | https://www.typescriptlang.org/docs/handbook/utility-types.html 2 | https://www.typescriptlang.org/docs/handbook/2/functions.html#function-overloads 3 | https://www.npmjs.com/package/analytics 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 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 | ### `yarn 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 | ### `yarn test` 18 | 19 | Launches the test runner in the interactive watch mode.\ 20 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 21 | 22 | ### `yarn build` 23 | 24 | Builds the app for production to the `build` folder.\ 25 | It correctly bundles React in production mode and optimizes the build for the best performance. 26 | 27 | The build is minified and the filenames include the hashes.\ 28 | Your app is ready to be deployed! 29 | 30 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 31 | 32 | ### `yarn eject` 33 | 34 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 35 | 36 | 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. 37 | 38 | 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. 39 | 40 | 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. 41 | 42 | ## Learn More 43 | 44 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 45 | 46 | To learn React, check out the [React documentation](https://reactjs.org/). 47 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ts-analytics", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "^5.11.4", 7 | "@testing-library/react": "^11.1.0", 8 | "@testing-library/user-event": "^12.1.10", 9 | "@types/jest": "^26.0.15", 10 | "@types/node": "^12.0.0", 11 | "@types/react": "^17.0.0", 12 | "@types/react-dom": "^17.0.0", 13 | "analytics": "^0.7.4", 14 | "react": "^17.0.1", 15 | "react-dom": "^17.0.1", 16 | "react-scripts": "4.0.3", 17 | "typescript": "^4.1.2", 18 | "web-vitals": "^1.0.1" 19 | }, 20 | "scripts": { 21 | "start": "react-scripts start", 22 | "build": "react-scripts build", 23 | "test": "react-scripts test", 24 | "eject": "react-scripts eject" 25 | }, 26 | "eslintConfig": { 27 | "extends": [ 28 | "react-app", 29 | "react-app/jest" 30 | ] 31 | }, 32 | "browserslist": { 33 | "production": [ 34 | ">0.2%", 35 | "not dead", 36 | "not op_mini all" 37 | ], 38 | "development": [ 39 | "last 1 chrome version", 40 | "last 1 firefox version", 41 | "last 1 safari version" 42 | ] 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jherr/ts-analytics/4aed0af28185c90f008472583932e7b9e37ef207/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jherr/ts-analytics/4aed0af28185c90f008472583932e7b9e37ef207/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jherr/ts-analytics/4aed0af28185c90f008472583932e7b9e37ef207/public/logo512.png -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 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 | } 26 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | margin: auto; 3 | width: 800px; 4 | } 5 | button { 6 | margin-left: 1rem; 7 | font-size: xx-large; 8 | padding: 0.5rem; 9 | border-radius: 0.5rem; 10 | } -------------------------------------------------------------------------------- /src/App.test.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { render, screen } from '@testing-library/react'; 3 | import App from './App'; 4 | 5 | test('renders learn react link', () => { 6 | render(); 7 | const linkElement = screen.getByText(/learn react/i); 8 | expect(linkElement).toBeInTheDocument(); 9 | }); 10 | -------------------------------------------------------------------------------- /src/App.tsx: -------------------------------------------------------------------------------- 1 | import "./App.css"; 2 | import UIWithBadAPI from "./UIWithBad"; 3 | import UIWithOverloads from "./UIWithOverloads"; 4 | import UIWithOverloadsAndEnums from "./UIWithOverloadsAndEnums"; 5 | import UIWithEventMap from "./UIWithEventMap"; 6 | import UIWithFuncs from "./UIWithFuncs"; 7 | 8 | function App() { 9 | return ( 10 |
11 | 12 | 13 | 14 | 15 | 16 |
17 | ); 18 | } 19 | 20 | export default App; 21 | -------------------------------------------------------------------------------- /src/UIWithBad.tsx: -------------------------------------------------------------------------------- 1 | import { useCallback } from "react"; 2 | import analytics from "./analytics-bad"; 3 | 4 | function UIWithBadAPI() { 5 | const onCheckout = useCallback(() => { 6 | analytics.track("checkout", { 7 | products: ["iPhone"], 8 | }); 9 | }, []); 10 | 11 | const onAddToCart = useCallback(() => { 12 | analytics.track("addToCart", { 13 | product: "iPhone", 14 | quantity: 2, 15 | }); 16 | }, []); 17 | 18 | const onViewItem = useCallback(() => { 19 | analytics.track("viewProduct", { 20 | product: "iPhone", 21 | referrer: "Android phone", 22 | }); 23 | }, []); 24 | 25 | return ( 26 |
27 |

UI With Bad API

28 | 29 | 30 | 31 |
32 | ); 33 | } 34 | 35 | export default UIWithBadAPI; 36 | -------------------------------------------------------------------------------- /src/UIWithEventMap.tsx: -------------------------------------------------------------------------------- 1 | import { useCallback } from "react"; 2 | import { trackEvent } from "./analytics-eventmap"; 3 | 4 | function UIWitEventMap() { 5 | const onCheckout = useCallback(() => { 6 | trackEvent("checkout", { 7 | products: ["iPhone"], 8 | }); 9 | }, []); 10 | 11 | const onAddToCart = useCallback(() => { 12 | trackEvent("addToCart", { 13 | product: "iPhone", 14 | quantity: 2, 15 | }); 16 | }, []); 17 | 18 | const onViewItem = useCallback(() => { 19 | trackEvent("viewProduct", { 20 | product: "iPhone", 21 | referrer: "Android phone", 22 | }); 23 | }, []); 24 | 25 | return ( 26 |
27 |

UI With Event Map

28 | 29 | 30 | 31 |
32 | ); 33 | } 34 | 35 | export default UIWitEventMap; 36 | -------------------------------------------------------------------------------- /src/UIWithFuncs.tsx: -------------------------------------------------------------------------------- 1 | import { useCallback } from "react"; 2 | import { addToCart, checkoutEvent, viewProductEvent } from "./analytics-funcs"; 3 | 4 | function UIWitEventMap() { 5 | const onCheckout = useCallback(() => { 6 | checkoutEvent({ 7 | products: ["iPhone"], 8 | }); 9 | }, []); 10 | 11 | const onAddToCart = useCallback(() => { 12 | addToCart({ 13 | product: "iPhone", 14 | quantity: 2, 15 | }); 16 | }, []); 17 | 18 | const onViewItem = useCallback(() => { 19 | viewProductEvent({ 20 | product: "iPhone", 21 | referrer: "Android phone", 22 | }); 23 | }, []); 24 | 25 | return ( 26 |
27 |

UI With Funcs

28 | 29 | 30 | 31 |
32 | ); 33 | } 34 | 35 | export default UIWitEventMap; 36 | -------------------------------------------------------------------------------- /src/UIWithOverloads.tsx: -------------------------------------------------------------------------------- 1 | import { useCallback } from "react"; 2 | import { trackEvent } from "./analytics-overloads"; 3 | 4 | function UIWithBadAPI() { 5 | const onCheckout = useCallback(() => { 6 | trackEvent("checkout", { 7 | products: ["iPhone"], 8 | }); 9 | }, []); 10 | 11 | const onAddToCart = useCallback(() => { 12 | trackEvent("addToCart", { 13 | product: "iPhone", 14 | quantity: 2, 15 | }); 16 | }, []); 17 | 18 | const onViewItem = useCallback(() => { 19 | trackEvent("viewProduct", { 20 | product: "iPhone", 21 | referrer: "Android phone", 22 | }); 23 | }, []); 24 | 25 | return ( 26 |
27 |

UI With Overloads

28 | 29 | 30 | 31 |
32 | ); 33 | } 34 | 35 | export default UIWithBadAPI; 36 | -------------------------------------------------------------------------------- /src/UIWithOverloadsAndEnums.tsx: -------------------------------------------------------------------------------- 1 | import { useCallback } from "react"; 2 | import { trackEvent, EventName } from "./analytics-overloads-enums"; 3 | 4 | function UIWithBadAPI() { 5 | const onCheckout = useCallback(() => { 6 | trackEvent(EventName.checkout, { 7 | products: ["iPhone"], 8 | }); 9 | }, []); 10 | 11 | const onAddToCart = useCallback(() => { 12 | trackEvent(EventName.addToCart, { 13 | product: "iPhone", 14 | quantity: 2, 15 | }); 16 | }, []); 17 | 18 | const onViewItem = useCallback(() => { 19 | trackEvent(EventName.viewProduct, { 20 | product: "iPhone", 21 | referrer: "Android phone", 22 | }); 23 | }, []); 24 | 25 | return ( 26 |
27 |

UI With Overloads and Enums

28 | 29 | 30 | 31 |
32 | ); 33 | } 34 | 35 | export default UIWithBadAPI; 36 | -------------------------------------------------------------------------------- /src/analytics-bad.ts: -------------------------------------------------------------------------------- 1 | import Analytics from "analytics"; 2 | 3 | const analytics = Analytics({ 4 | app: "my-app", 5 | debug: true, 6 | }); 7 | 8 | export default analytics; 9 | -------------------------------------------------------------------------------- /src/analytics-eventmap.ts: -------------------------------------------------------------------------------- 1 | import Analytics from "analytics"; 2 | 3 | interface CheckoutEventProps { 4 | products: string[]; 5 | } 6 | interface AddToCartEventProps { 7 | product: string; 8 | quantity: number; 9 | } 10 | interface ViewProductEventProps { 11 | product: string; 12 | referrer: string; 13 | } 14 | 15 | const analytics = Analytics({ 16 | app: "my-app", 17 | debug: true, 18 | }); 19 | 20 | export interface EventMap { 21 | checkout: CheckoutEventProps; 22 | addToCart: AddToCartEventProps; 23 | viewProduct: ViewProductEventProps; 24 | } 25 | 26 | export function trackEvent( 27 | eventName: K, 28 | props: EventMap[K] 29 | ): void { 30 | analytics.track(eventName, props); 31 | } 32 | -------------------------------------------------------------------------------- /src/analytics-funcs.ts: -------------------------------------------------------------------------------- 1 | import Analytics from "analytics"; 2 | 3 | interface CheckoutEventProps { 4 | products: string[]; 5 | } 6 | interface AddToCartEventProps { 7 | product: string; 8 | quantity: number; 9 | } 10 | interface ViewProductEventProps { 11 | product: string; 12 | referrer: string; 13 | } 14 | 15 | const analytics = Analytics({ 16 | app: "my-app", 17 | debug: true, 18 | }); 19 | 20 | function createAnalyticsSender(name: string) { 21 | return (props: T) => { 22 | analytics.track(name, props); 23 | }; 24 | } 25 | 26 | export const checkoutEvent = createAnalyticsSender( 27 | "checkout" 28 | ); 29 | export const viewProductEvent = createAnalyticsSender( 30 | "viewProduct" 31 | ); 32 | export const addToCart = createAnalyticsSender( 33 | "addToCart" 34 | ); 35 | -------------------------------------------------------------------------------- /src/analytics-overloads-enums.ts: -------------------------------------------------------------------------------- 1 | import Analytics from "analytics"; 2 | 3 | export enum EventName { 4 | checkout = "checkout", 5 | addToCart = "addToCart", 6 | viewProduct = "viewProduct", 7 | } 8 | 9 | interface CheckoutEventProps { 10 | products: string[]; 11 | } 12 | interface AddToCartEventProps { 13 | product: string; 14 | quantity: number; 15 | } 16 | interface ViewProductEventProps { 17 | product: string; 18 | referrer: string; 19 | } 20 | 21 | const analytics = Analytics({ 22 | app: "my-app", 23 | debug: true, 24 | }); 25 | 26 | export function trackEvent( 27 | eventName: EventName.checkout, 28 | props: CheckoutEventProps 29 | ): void; 30 | export function trackEvent( 31 | eventName: EventName.addToCart, 32 | props: AddToCartEventProps 33 | ): void; 34 | export function trackEvent( 35 | eventName: EventName.viewProduct, 36 | props: ViewProductEventProps 37 | ): void; 38 | export function trackEvent(eventName: EventName, props: {}): void { 39 | analytics.track(eventName, props); 40 | } 41 | -------------------------------------------------------------------------------- /src/analytics-overloads.ts: -------------------------------------------------------------------------------- 1 | import Analytics from "analytics"; 2 | 3 | interface CheckoutEventProps { 4 | products: string[]; 5 | } 6 | interface AddToCartEventProps { 7 | product: string; 8 | quantity: number; 9 | } 10 | interface ViewProductEventProps { 11 | product: string; 12 | referrer: string; 13 | } 14 | 15 | const analytics = Analytics({ 16 | app: "my-app", 17 | debug: true, 18 | }); 19 | 20 | export function trackEvent( 21 | eventName: "checkout", 22 | props: CheckoutEventProps 23 | ): void; 24 | export function trackEvent( 25 | eventName: "addToCart", 26 | props: AddToCartEventProps 27 | ): void; 28 | export function trackEvent( 29 | eventName: "viewProduct", 30 | props: ViewProductEventProps 31 | ): void; 32 | export function trackEvent(eventName: string, props: {}): void { 33 | analytics.track(eventName, props); 34 | } 35 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 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 | } 14 | -------------------------------------------------------------------------------- /src/index.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import reportWebVitals from './reportWebVitals'; 6 | 7 | ReactDOM.render( 8 | 9 | 10 | , 11 | document.getElementById('root') 12 | ); 13 | 14 | // If you want to start measuring performance in your app, pass a function 15 | // to log results (for example: reportWebVitals(console.log)) 16 | // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals 17 | reportWebVitals(); 18 | -------------------------------------------------------------------------------- /src/logo.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/react-app-env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | -------------------------------------------------------------------------------- /src/reportWebVitals.ts: -------------------------------------------------------------------------------- 1 | import { ReportHandler } from 'web-vitals'; 2 | 3 | const reportWebVitals = (onPerfEntry?: ReportHandler) => { 4 | if (onPerfEntry && onPerfEntry instanceof Function) { 5 | import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => { 6 | getCLS(onPerfEntry); 7 | getFID(onPerfEntry); 8 | getFCP(onPerfEntry); 9 | getLCP(onPerfEntry); 10 | getTTFB(onPerfEntry); 11 | }); 12 | } 13 | }; 14 | 15 | export default reportWebVitals; 16 | -------------------------------------------------------------------------------- /src/setupTests.ts: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom'; 6 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 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 | } 27 | --------------------------------------------------------------------------------