Coder Social home page Coder Social logo

react-example's Introduction

Segment's Example Javascript Source (React)

This project provides 2 examples of how to implement Segment's Javascript source (Analytics 2.0) in a React app:

Curious about Segment? You can learn about it more here.

Using this repository as a Segment Source

Supplementary documentation about a basic Segment instrumentation can be found here.

This site is currently deployed using GitHub Pages on http://segmentio.github.io/react-example/.

You can quickly deploy the same site and use this repository as a Segment Javascript Source so you can see data flowing through your debugger in real time:

  1. Fork this repository GH Fork Button

  2. Enable Github Actions for the repository through the Actions tab

    GH fork button After enabling the button, you should be able to see the Actions view GH empty Workflows page

  3. Grant the workflow correct repo permissions:

    • Go to the repository settings, and in the sidebar click Actions under the Code and automation section GH actions settings
    • Scroll down to see the workflow settings: GH workflow settings
    • Select read and write permissions and Save.
  4. Add your Segment Source write key to Github Secrets

    • Continuing in the repository settings, in the sidebar click Secrets under the Security section

    • Click New Repository Secret GH empty Secrets page

    • Add REACT_APP_SEGMENT_WRITE_KEY as the secret's name, with the source write key as the value GH create Secret page

      After adding the secret to Github, you should be able to see your Secret: GH Secrets page

  5. Configure Github Pages to use the gh-pages branch GH Pages

  6. Copy the URL provided by Github in the Pages option under Code and Automation section in the sidebar

  7. Change the homepage URL in the package.json file, using the URL copied from step 6. Make sure to commit your changes

    GH package.json GH edit package.json

  8. Navigate to the URL copied from step 6, and click around, and see data flowing through your Source! There might be a wait for the page to deploy. Average time should be around 1 minute.

What's going on under the hood?

When instrumented, clicking around your webpage sends events to Segment. You can see these calls in the Network tab in your browser's Developer Tools: Segment Track Header Segment Track Payload

You should be able to navigate to your Source's debugger and see events flowing in real time: Segment debugger

Troubleshooting Deploy

If, when clicking through the website, you are not seeing events flowing through the debugger and you do not see events being fired in Developer Tools, the Github Pages site might not be deployed correctly.

Issue: The workflow is not deployed in the correct order

The website is properly deployed if the latest workflow run is the Deploy to GH Pages workflow: GH Actions workflow runs

Sometimes, this is not the case, meaning that the SOURCE_WRITE_KEY environment variable was not correctly injected into the running App. If that happens, simply click the latest workflow run (in this case Update package.json), and rerun the workflow: GH Actions redeploy run

Note: relatedly, not being able to access Segment's analytics will not impact your App from running.

The JSX examples in this repo use Segment's design system, Evergreen. Check it out!

Available Scripts

This typescript project was bootstrapped with Create React App. You can learn more in the Create React App documentation. To learn React, check out the React documentation.

In the project directory, you can run:

yarn start

To run the page with analytics calls, run

REACT_APP_SEGMENT_WRITE_KEY=<write_key_here> yarn start

Runs the app in the development mode.
Open http://localhost:3000 to view it in the browser.

The page will reload if you make edits.
You will also see any lint errors in the console.

yarn test

Launches the test runner in the interactive watch mode.
See the section about running tests for more information.

yarn build

Builds the app for production to the build folder.
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.
Your app is ready to be deployed!

See the section about deployment for more information.

yarn eject

Note: this is a one-way operation. Once you eject, you can’t go back!

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.

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.

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.

yarn deploy

Builds & deploys to the Github Pages website specified in the package.json.

This repo has a Github Action workflow, but you can deploy to your Github Page straight from the terminal using this command.

See this section about deployment for more information.

react-example's People

Contributors

catligad avatar whieronymus avatar neztex avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.