Overview
The Dashgram React SDK provides first-class React integration for Telegram Mini Apps. It includes React hooks, context providers, and React-specific features for seamless analytics integration.This SDK is specifically designed for Telegram Mini Apps built with React. It requires your application to be
running inside Telegram’s WebApp environment.
Installation
Getting Started
1. Wrap Your App with DashgramProvider
Wrap your app withDashgramProvider at the root level:
2. Track Custom Events (optional)
If you want to send custom events to Dashgram, use theuseTrackEvent hook. Simply call it with an event name and optional properties whenever the action happens.
Complete Example
Track Levels
The React SDK supports the same three tracking levels as the JavaScript SDK:Level 1 - Core
Level 1 - Core
Minimal tracking. Only captures basic app lifecycle events:
app_open- When the Mini App is openedapp_close- When the Mini App is closed
Level 2 - Interactions (Recommended)
Level 2 - Interactions (Recommended)
Standard web analytics. Level 1 + user interactions:
- Page/route navigation
- Button clicks
- Form submissions
- Input field interactions
- Clipboard operations
- JavaScript errors
Level 3 - Deep Analytics
Level 3 - Deep Analytics
Comprehensive tracking. Level 1 + 2 + performance metrics + all Telegram events:
- Scroll depth tracking
- Element visibility
- Web Vitals (LCP, FID, CLS)
- Network status
- All Telegram WebApp events
- Performance monitoring
TypeScript Support
The SDK includes full TypeScript definitions:API Reference
<DashgramProvider>
Provider component that initializes the SDK and makes it available to all child components.
Props:
| Prop | Type | Required | Default | Description |
|---|---|---|---|---|
projectId | string | Yes | — | Your project ID from Dashgram dashboard |
trackLevel | 1 | 2 | 3 | No | 2 | Event collection level |
debug | boolean | No | false | Enable debug logging |
disabled | boolean | No | false | Disable all tracking |
batchSize | number | No | 10 | Number of events to batch before sending |
flushInterval | number | No | 5000 | Milliseconds between automatic flushes |
apiUrl | string | No | https://api.dashgram.io/v1 | Custom API endpoint |
onError | function | No | — | Callback for handling errors |
children | ReactNode | Yes | — | Your app components |
useDashgram()
Hook to access the SDK instance directly. Returns the full SDK context.
Returns:
useTrackEvent()
Hook that returns a function to track custom events. This is the recommended way to track events in React components.
Returns: (event: string, properties?: EventProperties) => void
Example:
usePageView(pageName, properties?)
Hook to track page views for SPA routing. Automatically prevents duplicate tracking for the same page.
Parameters:
pageName(string) - Page identifierproperties(object, optional) - Additional properties
useScreenTracking()
Hook to automatically track screen views when using React Router. Requires react-router-dom to be installed.
Example:
useTrackMount(event, properties?)
Hook to track when a component mounts.
Example:
useTrackUnmount(event, properties?)
Hook to track when a component unmounts.
Example:
useAutoTrack(componentName, properties?)
Hook to automatically track component lifecycle events. Tracks ${componentName}_mounted on mount and ${componentName}_unmounted on unmount.
Example:
my_component_mountedwhen the component mountsmy_component_unmountedwhen the component unmounts
useTrackClick(event, properties?)
Hook that returns a click handler function that tracks the event.
Example:
useTrackSubmit(event, properties?)
Hook that returns a form submit handler function that tracks the event.
Example:
Best Practices
1. Provider Placement
PlaceDashgramProvider at the root of your app, but inside your router if you’re using one:
2. Use Hooks Appropriately
- Use
useTrackEvent()for most custom events - Use
usePageView()for page/screen tracking - Use
useAutoTrack()for component lifecycle tracking - Use
useDashgram()when you need full SDK access
3. Avoid Tracking in Render
Don’t track events directly in render. Use hooks or event handlers:4. Memoize Event Handlers
For frequently re-rendered components, memoize event handlers:Troubleshooting
Provider Not Found Error
If you see “useDashgram must be used within DashgramProvider”:- Ensure
DashgramProviderwraps your component tree - Check that you’re importing from
@dashgram/react - Verify the component using the hook is a child of the provider
Events Not Sending
- Check initialization: Ensure
DashgramProviderhas a validprojectId - Verify Telegram environment: SDK only works inside Telegram WebApp
- Enable debug mode: Set
debug={true}onDashgramProvider - Check console: Look for Dashgram debug logs
Next Steps
- Learn about Track Levels to choose the right level for your app
- Check out the JavaScript SDK for vanilla JS usage
- View your analytics in the Dashgram Dashboard