### Install Dependencies and Start Dev Server Source: https://github.com/posthog/posthog-js/blob/main/examples/example-next-app-router/README.md Install dependencies and start the development server from the example directory. ```bash pnpm install pnpm dev ``` -------------------------------- ### Manual Setup: Install Dependencies and Start Server Source: https://github.com/posthog/posthog-js/blob/main/playground/remix/README.md After building and packaging PostHog libraries, navigate to the Remix playground directory, install its dependencies, and start the development server using pnpm. ```bash cd playground/remix pnpm install pnpm dev ``` -------------------------------- ### Start Example Development Server Source: https://github.com/posthog/posthog-js/blob/main/examples/example-expo-53/README.md Starts the development server for the example project. ```bash pnpm start ``` -------------------------------- ### Manual Setup: Install Dependencies and Run Dev Server Source: https://github.com/posthog/posthog-js/blob/main/playground/nextjs/README.md After building and packaging the SDK, run these commands from the playground directory to install its dependencies and start the development server. Configure PostHog host and key using environment variables. ```bash pnpm install NEXT_PUBLIC_POSTHOG_KEY='' NEXT_PUBLIC_POSTHOG_HOST='http://localhost:8010' pnpm dev ``` -------------------------------- ### Install Example Dependencies Source: https://github.com/posthog/posthog-js/blob/main/examples/example-expo-53/README.md Installs dependencies for the example project. Use --no-lockfile to avoid updating the lockfile. ```bash pnpm install # you can also avoid lockfile changes pnpm install --no-lockfile ``` -------------------------------- ### Install Project Dependencies Source: https://github.com/posthog/posthog-js/blob/main/examples/example_rn_macos/README.md Run these commands to install the required tools and dependencies before running the example. ```bash npx react-native-macos-init ``` ```bash sudo gem install cocoapods ``` ```bash npm install --global yarn ``` ```bash npm install --global rollup ``` -------------------------------- ### Install and Run Development Server Source: https://github.com/posthog/posthog-js/blob/main/packages/browser/playground/redux-todo-list/README.md Use these commands to install dependencies and start the local development environment for the todo application. ```bash pnpm i && pnpm dev ``` -------------------------------- ### Install SDK Dependencies Source: https://github.com/posthog/posthog-js/blob/main/examples/example-node/README.md Build and package the SDK, then install example dependencies for the Node.js environment. ```bash # Build and package the SDK pnpm package # Install example dependencies cd examples/example-node pnpm install ``` -------------------------------- ### Run All Examples Quick Start Source: https://github.com/posthog/posthog-js/blob/main/examples/example-node/README.md Execute all PostHog Node.js SDK capabilities in sequence by choosing option 5 from the interactive menu. ```bash pnpm run example # Choose option 5: "Run all examples" ``` -------------------------------- ### Install and Run Example Convex App Source: https://github.com/posthog/posthog-js/blob/main/examples/example-convex/README.md Commands to install dependencies, build the PostHog-js tarball, and run the example Convex app from the repository root. ```sh pnpm i pnpm package # builds the tarball that this app installs pnpm dev --filter example-convex ``` -------------------------------- ### Setup Project Environment Source: https://github.com/posthog/posthog-js/blob/main/examples/example-ai-anthropic/README.md Install dependencies and configure environment variables for the project. ```bash pnpm install cp .env.example .env # Fill in your API keys in .env ``` -------------------------------- ### Run PostHog.js Example Source: https://github.com/posthog/posthog-js/blob/main/examples/example_rn_macos/README.md Execute these make commands to install and run the PostHog.js example after setting up dependencies. ```bash make install make run ``` -------------------------------- ### Install Dependencies and Setup Environment Source: https://github.com/posthog/posthog-js/blob/main/examples/example-ai-gemini/README.md Installs project dependencies and copies the environment file. Ensure to fill in your API keys in the .env file. ```bash pnpm install cp .env.example .env # Fill in your API keys in .env ``` -------------------------------- ### Install Dependencies and Configure Environment Source: https://github.com/posthog/posthog-js/blob/main/examples/example-spacetimedb/README.md Navigate to the example directory, install dependencies, and copy the environment file. Set your PostHog project API key in the `.env.local` file. ```sh cd examples/example-spacetimedb pnpm install cp .env.example .env.local ``` -------------------------------- ### Install Dependencies and Run Development Server Source: https://github.com/posthog/posthog-js/blob/main/packages/convex/CONTRIBUTING.md Run these commands from the repository root to install dependencies and start the development server. ```sh pnpm i pnpm dev ``` -------------------------------- ### Run Remote Config Example Source: https://github.com/posthog/posthog-js/blob/main/playground/flags/README.md Commands to install dependencies and execute the remote configuration example script. ```bash # Install dependencies npm install # Run the remote config example npm run remote-config # or node remote-config-example.js ``` -------------------------------- ### Start Web Development Server Source: https://github.com/posthog/posthog-js/blob/main/examples/example-expo-53/README.md Starts the web development server for the example. Ensure 'persistence: "memory"' is uncommented in posthog.tsx for full functionality. ```bash npx expo start --web ``` -------------------------------- ### Setup Project Environment Source: https://github.com/posthog/posthog-js/blob/main/examples/example-ai-ollama/README.md Install dependencies and configure environment variables for the Ollama and PostHog integration. ```bash pnpm install cp .env.example .env # Fill in your API keys in .env # Make sure Ollama is running locally: ollama serve ``` -------------------------------- ### Run Integration Examples Source: https://github.com/posthog/posthog-js/blob/main/examples/example-ai-anthropic/README.md Execute the chat and streaming examples using the configured environment. ```bash source .env npx tsx chat.ts npx tsx streaming.ts ``` -------------------------------- ### Run Automated Setup Script Source: https://github.com/posthog/posthog-js/blob/main/playground/remix/README.md Execute this bash script for a quick setup of the PostHog Remix playground. It handles building PostHog packages, creating tarballs, setting up symlinks, installing dependencies, and starting the dev server. ```bash ./bin/localdev.sh ``` -------------------------------- ### Install iOS Pods Source: https://github.com/posthog/posthog-js/blob/main/examples/example-expo-53/README.md Installs the necessary CocoaPods dependencies for the iOS example project. ```bash cd ios pod install cd .. ``` -------------------------------- ### Start SpacetimeDB Server Source: https://github.com/posthog/posthog-js/blob/main/examples/example-spacetimedb/README.md Initiate the local SpacetimeDB server. This is the first step in running the example. ```sh spacetime start ``` -------------------------------- ### Install dependencies and configure environment Source: https://github.com/posthog/posthog-js/blob/main/examples/example-ai-aws-bedrock/README.md Prepare the project environment by installing dependencies and setting up API keys. ```bash pnpm install cp .env.example .env # Fill in your API keys in .env # Ensure your AWS credentials are configured ``` -------------------------------- ### Manual Setup: Build and Package PostHog JS SDK Source: https://github.com/posthog/posthog-js/blob/main/playground/nextjs/README.md Manually build and package the PostHog JS SDK from the repository root before setting up the playground. This involves installing dependencies, building all packages, and creating tarballs. ```bash pnpm install pnpm build pnpm package ``` -------------------------------- ### Run AI Examples Source: https://github.com/posthog/posthog-js/blob/main/examples/example-ai-vercel-ai/README.md Execute the AI examples using tsx after setting up your environment variables. ```bash source .env npx tsx generate-text.ts npx tsx stream-text.ts npx tsx generate-object.ts ``` -------------------------------- ### Install @rrweb/all Source: https://github.com/posthog/posthog-js/blob/main/packages/rrweb/all/README.md Use npm to install the @rrweb/all package. ```bash npm install @rrweb/all ``` -------------------------------- ### Install @rrweb/replay Source: https://github.com/posthog/posthog-js/blob/main/packages/rrweb/replay/README.md Install the @rrweb/replay package using npm. ```bash npm install @rrweb/replay ``` -------------------------------- ### Run Example on iOS and Android Simulators Source: https://github.com/posthog/posthog-js/blob/main/examples/example-expo-53/README.md Commands to run the example application on iOS and Android simulators or emulators using Expo. ```bash npx expo run:ios npx expo run:android ``` -------------------------------- ### Install Dependencies Source: https://github.com/posthog/posthog-js/blob/main/packages/browser/playground/csp-violations/README.md Install the necessary project dependencies using pnpm. ```bash pnpm i ``` -------------------------------- ### Install @rrweb/rrweb-plugin-sequential-id-replay Source: https://github.com/posthog/posthog-js/blob/main/packages/rrweb/plugins/rrweb-plugin-sequential-id-replay/README.md Install the plugin using npm. This command downloads and installs the necessary package for your project. ```bash npm install @rrweb/rrweb-plugin-sequential-id-replay ``` -------------------------------- ### Example .env File Content Source: https://github.com/posthog/posthog-js/blob/main/examples/example-node/diagnostics/README.md This is an example of the content expected in the .env file for configuring PostHog connection details and test flags. ```env POSTHOG_PROJECT_API_KEY=phc_your_project_api_key_here POSTHOG_PERSONAL_API_KEY=phx_your_personal_api_key_here POSTHOG_HOST=https://app.posthog.com POSTHOG_TEST_FLAG_KEY=beta-feature ``` -------------------------------- ### Run the Node.js Example Source: https://github.com/posthog/posthog-js/blob/main/examples/example-node/README.md Execute the interactive PostHog Node.js SDK demo using pnpm or tsx. ```bash # Run the interactive example pnpm run example # Or use tsx directly npx tsx example.ts ``` -------------------------------- ### Install Local Dependencies Source: https://github.com/posthog/posthog-js/blob/main/examples/example-cloudflare-kv-cache/README.md Install project dependencies and package the application locally. ```bash pnpm i pnpm package ``` -------------------------------- ### Install @rrweb/record Source: https://github.com/posthog/posthog-js/blob/main/packages/rrweb/record/README.md Install the @rrweb/record package using npm. ```bash npm install @rrweb/record ``` -------------------------------- ### Configure Environment Variables Source: https://github.com/posthog/posthog-js/blob/main/examples/example-cloudflare-kv-cache/README.md Copy the example environment file and populate it with your PostHog API keys and host. ```bash cp .env.example .env ``` ```bash POSTHOG_PROJECT_API_KEY=your_project_api_key POSTHOG_PERSONAL_API_KEY=your_posthog_personal_api_key POSTHOG_HOST=https://us.i.posthog.com ``` -------------------------------- ### Install Dependencies with npm Source: https://github.com/posthog/posthog-js/blob/main/packages/nuxt/CONTRIBUTING.md Use this command to install dependencies within the playground directory. ```bash npm i ``` -------------------------------- ### Install Dependencies Source: https://github.com/posthog/posthog-js/blob/main/CONTRIBUTING.md Run these commands at the repository root to install dependencies using pnpm and ensure a consistent environment with nvm. ```sh nvm use pnpm install --frozen-lockfile ``` -------------------------------- ### Run Gemini API Examples Source: https://github.com/posthog/posthog-js/blob/main/examples/example-ai-gemini/README.md Executes example scripts for interacting with the Google Gemini API. Make sure to source your .env file first to load API keys. ```bash source .env npx tsx chat.ts npx tsx streaming.ts npx tsx image-generation.ts ``` -------------------------------- ### Run Evaluation Tags Example via Local Build Source: https://github.com/posthog/posthog-js/blob/main/playground/flags/README.md Commands to build the PostHog SDK locally and serve the evaluation tags example. ```bash # First, build PostHog locally from the root directory cd ../.. # Go to repository root pnpm build # Then serve the example cd playground/flags npm run serve # Edit evaluation-tags-example.html to uncomment the local build script tag # and comment out the CDN script ``` -------------------------------- ### Manual Setup: Build and Package PostHog Source: https://github.com/posthog/posthog-js/blob/main/playground/remix/README.md Manually build and package PostHog libraries from the repository root using pnpm commands. This is a prerequisite for the manual setup of the Remix playground. ```bash cd ../.. pnpm build pnpm package ``` -------------------------------- ### Install Dependencies and Build Source: https://github.com/posthog/posthog-js/blob/main/packages/browser/playground/error-tracking/README.md Installs project dependencies and builds the local version of posthog-js. ```shell # Install deps pnpm install # Build local version of posthog-js pnpm run build-posthog ``` -------------------------------- ### Run Node.js Evaluation Tags Example Source: https://github.com/posthog/posthog-js/blob/main/playground/flags/README.md Command to execute the Node.js evaluation tags example, which currently displays a warning as it is not yet supported. ```bash npm run evaluation-tags ``` -------------------------------- ### Build Playground with npm Source: https://github.com/posthog/posthog-js/blob/main/packages/nuxt/CONTRIBUTING.md Execute this command to build the playground after installing its dependencies. ```bash npm run build ``` -------------------------------- ### Run Instructor + PostHog AI Example Source: https://github.com/posthog/posthog-js/blob/main/examples/example-ai-instructor/README.md Source your environment variables and run the TypeScript example using tsx. This executes the Instructor extraction with PostHog tracking. ```bash source .env npx tsx extract.ts ``` -------------------------------- ### Run Evaluation Tags Example via CDN Source: https://github.com/posthog/posthog-js/blob/main/playground/flags/README.md Commands to serve the browser-based evaluation tags example using the CDN version of the PostHog SDK. ```bash # The HTML file now uses PostHog from CDN by default npm run serve # Then open http://localhost:8080/evaluation-tags-example.html in your browser ``` -------------------------------- ### Start Development Watch Mode Source: https://github.com/posthog/posthog-js/blob/main/CONTRIBUTING.md Run this command from the repository root to start the development server in watch mode, enabling automatic rebuilding on code changes. ```sh pnpm dev ``` -------------------------------- ### Launch Cypress Test Runner Source: https://github.com/posthog/posthog-js/blob/main/packages/browser/CONTRIBUTING.md Start the test server and then launch the Cypress test runner for end-to-end testing. This requires `pnpm start` to be running in a separate terminal. ```bash pnpm start pnpm cypress ``` -------------------------------- ### Run Local Development Server (Quick Start) Source: https://github.com/posthog/posthog-js/blob/main/playground/nextjs/README.md Execute this script from the playground directory to build, package, and run the development server for testing local SDK changes. Ensure you are in the 'playground/nextjs' directory. ```bash cd playground/nextjs ./bin/localdev.sh ``` -------------------------------- ### Using PostHog-js with npm/yarn Source: https://github.com/posthog/posthog-js/blob/main/packages/types/README.md When installing PostHog-js via a package manager, types are automatically included. This example shows basic initialization and event capture with full type safety. ```typescript // Types are already available when you install posthog-js import posthog from 'posthog-js' posthog.init('your-api-key') posthog.capture('my_event') // ✅ Fully typed ``` -------------------------------- ### Start Frontend Development Server Source: https://github.com/posthog/posthog-js/blob/main/examples/example-spacetimedb/README.md Launch the frontend development server to serve the application. Access the application at http://localhost:5173. ```sh pnpm dev ``` -------------------------------- ### Set up Development Environment for PostHog Source: https://github.com/posthog/posthog-js/blob/main/packages/browser/CONTRIBUTING.md Initialize the development environment for the main PostHog repository, including setting up a demo account. This is a prerequisite for testing the Next.js example locally. ```bash python manage.py setup_dev --no-data ``` -------------------------------- ### Install Dependencies for PostHog Nuxt Demo Source: https://github.com/posthog/posthog-js/blob/main/packages/browser/playground/nuxtjs/README.md Run npm install to download and install all necessary packages for the Nuxt project, including PostHog and its dependencies. ```bash npm install ``` -------------------------------- ### Initialize Test Environment Source: https://github.com/posthog/posthog-js/blob/main/examples/example-sdk_dr/test-feature-flag-misconfiguration.html Sets up the test environment by fetching the latest SDK version and initializing PostHog, providing a menu of available tests. ```javascript window.addEventListener('load', async () => { log('🚀 Feature flag misconfiguration test loaded', 'info'); await fetchLatestVersions(); log(`Using PostHog JS SDK version: ${currentVersion}`, 'info'); // Initialize PostHog with the fetched latest version initPostHogWithLatestVersion(); log('Ready to test feature flag misconfigurations!', 'success'); log('📋 Available tests:', 'info'); log(' • Flag Before Init - Calls flag before posthog.init()', 'info'); log(' • Multiple Flags Before Init - Calls several flags before init', 'info'); log(' • Proper Flag Usage - Initializes first, then calls flags', 'info'); log(''); updateCounts(); }); ``` -------------------------------- ### Configure PostHog Credentials Source: https://github.com/posthog/posthog-js/blob/main/examples/example-node/README.md Copy the example environment file and fill in your PostHog credentials for API keys and host. ```bash cp .env.example .env ``` ```bash # Your project API key (found on the /setup page in PostHog) POSTHOG_PROJECT_API_KEY=phc_your_project_api_key_here # Your personal API key (for local evaluation and other advanced features) POSTHOG_PERSONAL_API_KEY=phx_your_personal_api_key_here # PostHog host URL (remove this line if using posthog.com) POSTHOG_HOST=https://app.posthog.com ``` -------------------------------- ### Install PostHog CLI Source: https://github.com/posthog/posthog-js/blob/main/packages/browser/playground/error-tracking/README.md Installs the PostHog CLI globally using cargo. ```shell # Install PostHog CLI globally cargo install posthog-cli ``` -------------------------------- ### Install Dependencies with pnpm Source: https://github.com/posthog/posthog-js/blob/main/packages/nuxt/CONTRIBUTING.md Use this command to install dependencies within the module directory. ```bash pnpm i ``` -------------------------------- ### Install Workspace Dependencies Source: https://github.com/posthog/posthog-js/blob/main/AGENTS.md Installs all dependencies for the pnpm workspace. Ensure you are in the repository root. ```bash nvm use pnpm install ``` -------------------------------- ### Initialize and Play Replayer Source: https://github.com/posthog/posthog-js/blob/main/packages/rrweb/replay/README.md Import the Replayer class and initialize it with recorded events and options. Call the play() method to start replaying. ```js import { Replayer } from '@posthog/rrweb-replay'; const replayer = new Replayer(events, { // options }); replayer.play(); ``` -------------------------------- ### Install PostHog Next.js Config Source: https://github.com/posthog/posthog-js/blob/main/examples/example-nextjs/README.md Use this command to install the required package for PostHog integration in Next.js. ```bash npm install @posthog/nextjs-config ``` -------------------------------- ### Start Metro Bundler Source: https://github.com/posthog/posthog-js/blob/main/examples/example-rn-native-plugin/README.md Run this command to start the Metro JavaScript bundler, which is essential for React Native development. ```sh # Using npm npm start # OR using Yarn yarn start ``` -------------------------------- ### Flag Dependencies Demo Output Source: https://github.com/posthog/posthog-js/blob/main/examples/example-node/README.md Example output demonstrating local evaluation of flag dependencies, showing results for different user conditions. ```plaintext ============================================================ FLAG DEPENDENCIES EXAMPLES ============================================================ 🔗 Testing flag dependencies with local evaluation... ✅ @example.com user (test-flag-dependency): true ❌ Regular user (test-flag-dependency): false 🎯 Results Summary: - Flag dependencies evaluated locally: ✅ YES - Zero API calls needed: ✅ YES - Node.js SDK supports flag dependencies: ✅ YES ``` -------------------------------- ### Install pnpm Globally Source: https://github.com/posthog/posthog-js/blob/main/packages/browser/CONTRIBUTING.md Install the Performant Package Manager (pnpm) globally on your system. This is a prerequisite for linking local packages. ```bash npm install -g pnpm ``` -------------------------------- ### Initialize PostHog Slim Bundle and Extensions Source: https://github.com/posthog/posthog-js/blob/main/packages/browser/playground/slim-bundle/index.html Imports the slim bundle and all available extension bundles, assigning them to the window object for global access. ```javascript import posthog from '/dist/module.slim.js' import { AllExtensions, FeatureFlagsExtensions, ErrorTrackingExtensions, ToolbarExtensions, SurveysExtensions, SessionReplayExtensions, AnalyticsExtensions, ExperimentsExtensions, ConversationsExtensions, LogsExtensions, ProductToursExtensions, SiteAppsExtensions, TracingExtensions, } from '/dist/extension-bundles.js' window.posthog = posthog window.AllExtensions = AllExtensions window.FeatureFlagsExtensions = FeatureFlagsExtensions window.ErrorTrackingExtensions = ErrorTrackingExtensions window.ToolbarExtensions = ToolbarExtensions window.SurveysExtensions = SurveysExtensions window.SessionReplayExtensions = SessionReplayExtensions window.AnalyticsExtensions = AnalyticsExtensions window.ExperimentsExtensions = ExperimentsExtensions window.ConversationsExtensions = ConversationsExtensions window.LogsExtensions = LogsExtensions window.ProductToursExtensions = ProductToursExtensions window.SiteAppsExtensions = SiteAppsExtensions window.TracingExtensions = TracingExtensions window.__slim_bundle_ready = true ``` -------------------------------- ### Install rrweb Sequential ID Record Plugin Source: https://github.com/posthog/posthog-js/blob/main/packages/rrweb/plugins/rrweb-plugin-sequential-id-record/README.md Install the plugin using npm. This command adds the necessary package to your project dependencies. ```bash npm install @rrweb/rrweb-plugin-sequential-id-record ```