UNPKG

humanbehavior-js

Version:

SDK for HumanBehavior session and event recording

381 lines (296 loc) • 9.81 kB
# HumanBehavior SDK Auto-Installation Wizard The HumanBehavior SDK includes an intelligent auto-installation wizard that can detect your project's framework and automatically integrate the SDK with minimal user intervention. ## šŸš€ Quick Start ### Single Command Installation **When the package is published to npm:** ```bash npx humanbehavior-js auto-install YOUR_API_KEY ``` **For local development/testing:** ```bash node path/to/humanbehavior-js/dist/cli/auto-install.js YOUR_API_KEY ``` That's it! The wizard will: - šŸ” Auto-detect your project's framework - šŸ“¦ Install the humanbehavior-js package - āœļø Modify your codebase to integrate the SDK - šŸ”§ Create environment files with your API key - šŸš€ Make your app ready to track user behavior ## šŸ“‹ Supported Frameworks The wizard automatically detects and configures: - āœ… **React** (CRA, Vite, Webpack) - āœ… **Next.js** (App Router, Pages Router) - āœ… **Vue** (Vue CLI, Vite) - āœ… **Angular** - āœ… **Svelte** (SvelteKit, Vite) - āœ… **Nuxt.js** - āœ… **Vanilla JS/TS** - āœ… **Node.js** (CommonJS & ESM) ## šŸ› ļø Installation Options ### Basic Usage **When the package is published to npm:** ```bash # Interactive mode (prompts for API key) npx humanbehavior-js auto-install # With API key as argument npx humanbehavior-js auto-install your-api-key-here # Skip all prompts npx humanbehavior-js auto-install your-api-key-here --yes ``` **For local development/testing:** ```bash # Interactive mode (prompts for API key) node path/to/humanbehavior-js/dist/cli/auto-install.js # With API key as argument node path/to/humanbehavior-js/dist/cli/auto-install.js your-api-key-here # Skip all prompts node path/to/humanbehavior-js/dist/cli/auto-install.js your-api-key-here --yes ``` ### Advanced Options **When the package is published to npm:** ```bash # Specify project directory npx humanbehavior-js auto-install your-api-key -p /path/to/project # Dry run (show what would be changed without making changes) npx humanbehavior-js auto-install your-api-key --dry-run # Get help npx humanbehavior-js auto-install --help ``` **For local development/testing:** ```bash # Specify project directory node path/to/humanbehavior-js/dist/cli/auto-install.js your-api-key -p /path/to/project # Dry run (show what would be changed without making changes) node path/to/humanbehavior-js auto-install.js your-api-key --dry-run # Get help node path/to/humanbehavior-js/dist/cli/auto-install.js --help ``` ## šŸ”§ Framework-Specific Integration ### React ```jsx // The wizard automatically wraps your app with HumanBehaviorProvider import { HumanBehaviorProvider } from 'humanbehavior-js/react'; function App() { return ( <HumanBehaviorProvider apiKey={process.env.HUMANBEHAVIOR_API_KEY}> {/* Your app content */} </HumanBehaviorProvider> ); } ``` ### Next.js (App Router) ```tsx // Creates app/providers.tsx 'use client'; import { HumanBehaviorProvider } from 'humanbehavior-js/react'; export function Providers({ children }: { children: React.ReactNode }) { return ( <HumanBehaviorProvider apiKey={process.env.HUMANBEHAVIOR_API_KEY}> {children} </HumanBehaviorProvider> ); } // Modifies app/layout.tsx to use Providers ``` ### Next.js (Pages Router) ```tsx // Creates components/providers.tsx 'use client'; import { HumanBehaviorProvider } from 'humanbehavior-js/react'; export function Providers({ children }: { children: React.ReactNode }) { return ( <HumanBehaviorProvider apiKey={process.env.HUMANBEHAVIOR_API_KEY}> {children} </HumanBehaviorProvider> ); } // Modifies pages/_app.tsx to use Providers ``` ### Vue ```js // The wizard adds the plugin to your main.js/ts import { HumanBehaviorPlugin } from 'humanbehavior-js/vue'; app.use(HumanBehaviorPlugin, { apiKey: import.meta.env.VITE_HUMANBEHAVIOR_API_KEY }); ``` ### Angular ```typescript // The wizard adds the module to app.module.ts import { HumanBehaviorModule } from 'humanbehavior-js/angular'; @NgModule({ imports: [ // ... other imports HumanBehaviorModule.forRoot({ apiKey: environment.humanBehaviorApiKey }) ] }) ``` ### Svelte ```javascript // The wizard adds the store to your main file import { humanBehaviorStore } from 'humanbehavior-js/svelte'; humanBehaviorStore.init('your-api-key'); ``` ### Nuxt.js ```typescript // Creates plugins/humanbehavior.client.ts import { HumanBehaviorPlugin } from 'humanbehavior-js/vue'; export default defineNuxtPlugin((nuxtApp) => { nuxtApp.vueApp.use(HumanBehaviorPlugin, { apiKey: useRuntimeConfig().public.humanBehaviorApiKey }); }); ``` ## šŸ” Environment Variables The wizard intelligently handles environment files: ### Smart File Detection The wizard checks for existing environment files in this order: 1. `.env.local` 2. `.env.development.local` 3. `.env.development` 4. `.env.local.development` 5. `.env` 6. `.env.production` 7. `.env.staging` ### Framework-Specific Variables - **React/Next.js**: `HUMANBEHAVIOR_API_KEY` - **Vue**: `VITE_HUMANBEHAVIOR_API_KEY` - **Svelte**: `PUBLIC_HUMANBEHAVIOR_API_KEY` - **Angular**: `humanBehaviorApiKey` (in environment.ts) - **Nuxt**: `HUMANBEHAVIOR_API_KEY` ### Safe Environment Handling - āœ… **Preserves existing files** (never overwrites) - āœ… **Appends to existing files** (adds API key safely) - āœ… **Skips duplicates** (won't add same variable twice) - āœ… **Works with gitignored files** (finds .env files even if ignored) ## šŸ“ Project Structure After Installation ### React/Next.js Project ``` your-project/ ā”œā”€ā”€ .env.local # Created with API key ā”œā”€ā”€ src/ │ ā”œā”€ā”€ App.tsx # Modified with provider │ └── index.tsx # Unchanged ā”œā”€ā”€ package.json # humanbehavior-js added └── node_modules/ ``` ### Next.js App Router ``` your-project/ ā”œā”€ā”€ .env.local # Created with API key ā”œā”€ā”€ app/ │ ā”œā”€ā”€ layout.tsx # Modified to use providers │ └── providers.tsx # Created with provider ā”œā”€ā”€ package.json # humanbehavior-js added └── node_modules/ ``` ### Vue Project ``` your-project/ ā”œā”€ā”€ .env.local # Created with API key ā”œā”€ā”€ src/ │ └── main.js # Modified with plugin ā”œā”€ā”€ package.json # humanbehavior-js added └── node_modules/ ``` ## šŸŽÆ What Gets Modified ### Code Changes - **Main entry point**: Wrapped with provider/plugin - **Environment files**: API key added safely - **Package.json**: humanbehavior-js dependency added ### Files Created - **Environment files**: `.env.local`, `.env`, etc. - **Provider files**: `providers.tsx` (Next.js) - **Plugin files**: `humanbehavior.client.ts` (Nuxt) ### Files Modified - **Main app file**: `App.tsx`, `main.js`, `layout.tsx`, etc. - **Package.json**: Dependencies updated - **Environment files**: API key appended ## 🚨 Troubleshooting ### Common Issues **"Framework not detected"** - Ensure you're running the command from your project root - Check that `package.json` exists and has dependencies **"Permission denied"** - Make sure you have write permissions to your project directory - Try running with `sudo` if needed (not recommended) **"Package installation failed"** - Check your internet connection - Ensure npm/yarn is properly configured - Try running `npm install` manually first ### Manual Fallback If the wizard fails, you can install manually: ```bash # Install the package npm install humanbehavior-js # Add environment variable echo "HUMANBEHAVIOR_API_KEY=your-api-key" >> .env.local # Then follow framework-specific setup in the main README ``` ## šŸš€ Current Status **Note**: The auto-installation wizard is currently in development and not yet published to npm. To test it locally: 1. **Clone the repository**: ```bash git clone https://github.com/humanbehavior-gh/humanbehavior-js.git cd humanbehavior-js ``` 2. **Build the wizard**: ```bash npm install npm run build ``` 3. **Test in your project**: ```bash node dist/cli/auto-install.js your-api-key --yes ``` Once the package is published to npm, you'll be able to use the `npx` commands shown above. ## šŸ” Debugging ### Dry Run Mode **When the package is published to npm:** ```bash npx humanbehavior-js auto-install your-api-key --dry-run ``` **For local development/testing:** ```bash node path/to/humanbehavior-js/dist/cli/auto-install.js your-api-key --dry-run ``` Shows what changes would be made without actually making them. ### Verbose Output The wizard provides detailed output showing: - Framework detection results - Package installation status - Files created/modified - Next steps for the user ## šŸ“š Next Steps After Installation Once the wizard completes: 1. **Start your development server** ```bash npm start # or yarn dev ``` 2. **Verify integration** - Check browser console for SDK initialization - Visit your app and trigger some interactions - Check your HumanBehavior dashboard for sessions 3. **Customize tracking** (optional) ```javascript // React import { useHumanBehavior } from 'humanbehavior-js/react'; function MyComponent() { const { trackEvent } = useHumanBehavior(); const handleClick = () => { trackEvent('button_clicked', { button: 'submit' }); }; } ``` ## šŸŽ‰ Success Indicators The wizard is successful when you see: - āœ… "Installation completed successfully!" - āœ… Framework detected correctly - āœ… Package installed without errors - āœ… Environment file created - āœ… Code modifications applied Your app is now ready to track user behavior! šŸš€