> ## Documentation Index
> Fetch the complete documentation index at: https://docs.telemetree.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Node.js

> Set up your Telemetree Node.js SDK

<img className="block dark:hidden" src="https://mintcdn.com/telemetree/akp6XNv3V6IKBA56/images/tele-hero-light.svg?fit=max&auto=format&n=akp6XNv3V6IKBA56&q=85&s=c92236583b01bcf1c327de87393c9b11" alt="Hero Light" width="700" height="320" data-path="images/tele-hero-light.svg" />

<img className="hidden dark:block" src="https://mintcdn.com/telemetree/akp6XNv3V6IKBA56/images/tele-hero-dark.svg?fit=max&auto=format&n=akp6XNv3V6IKBA56&q=85&s=6c753a4fcf02917cda0ae770ed798fe5" alt="Hero Dark" width="700" height="320" data-path="images/tele-hero-dark.svg" />

<Note> [Github](https://github.com/Telemetree/telemetree-node) | [Tutorial](https://github.com/Telemetree/telemetree-node-example) | [NPM](https://www.npmjs.com/package/@tonsolutions/telemetree-node) </Note>
Telemetree is a comprehensive free analytics tool designed specifically for **Telegram Mini Apps**. With our SDKs, developers, marketers, and product managers can easily track and optimize user engagement, making data-driven decisions to boost user acquisition and retention. Telemetree simplifies **Analytics for Telegram Mini Apps** by delivering insights into user behaviors, acquisition channels, and in-app interactions.

## Installation

<Note>    [NodeJS example](https://github.com/Telemetree/telemetree-node-example) to jumpstart using Telemetree</Note>

```bash theme={null}
npm install @tonsolutions/telemetree-node
```

## Quick Start

1. First, obtain your Telemetree API credentials from your [application settings](https://app.telemetree.io/settings/project).
   * `Project ID`
   * `API Key`

2. Initialize the Telemetree client in your bot:

```javascript theme={null}
const { TelemetreeClient } = require('@tonsolutions/telemetree-node');

const telemetree = new TelemetreeClient(
    process.env.TELEMETREE_PROJECT_ID,
    process.env.TELEMETREE_API_KEY
);

// Initialize the client before using
await telemetree.initialize();
```

3. Set up event tracking in your bot handlers:

```javascript theme={null}
// Example with node-telegram-bot-api
bot.on('message', async (msg) => {
    try {
        // Track the update
        const response = await telemetree.trackUpdate(msg);
        console.log('Tracking response:', response);
    } catch (error) {
        console.error('Failed to track message:', error);
    }
});
```

## Configuration

Create a `.env` file with your Telemetree credentials:

```
TELEMETREE_PROJECT_ID=your-project-id
TELEMETREE_API_KEY=your-api-key
```

## Event Tracking

The SDK automatically tracks various Telegram events:

* Messages
* Edited messages
* Commands
* Inline queries
* And more...

### Custom Event Tracking

You can also track custom events:

```javascript theme={null}
await telemetree.trackEvent({
    event_type: 'custom_event',
    event_data: {
        // your custom data
    }
});
```

## Error Handling

The SDK provides comprehensive error handling:

```javascript theme={null}
try {
    await telemetree.trackUpdate(update);
} catch (error) {
    if (error.name === 'CustomEventNotSupported') {
        // Handle unsupported event
    } else {
        // Handle other errors
    }
}
```

## Response Logging

The SDK provides detailed response logging:

```javascript theme={null}
{
    success: true/false,
    status: 200,
    statusText: 'OK',
    data: {...},
    headers: {...},
    requestTime: '2024-11-26T01:48:24.745Z'
}
```

### Encryption

The SDK uses RSA encryption to secure event data before sending it to the Telemetree service, ensuring data privacy. The `publicKey` is fetched automatically from the Telemetree configuration service during initialization, so there’s no need to manually set it.

## Key Features

* **Real-Time Analytics**: Monitor user activity within your Telegram Mini App in real-time.
* **User Retention Metrics**: Track returning users and pinpoint which features encourage app retention.
* **Web3 data**: discover web3 metrics associated with your users.
* **Seamless Integration**: Our SDKs are lightweight and integrate easily with auto event mapping.
* **Telegram-native**: Telemetree is built natively for Telegram.
* **User segmentation**: API for personalized notifications based on cohorts, completed actions. web3 data and more.
* **Free tier** with wide limits

## Resources

Consider visiting our resources for more info about the state of the Telegram Mini Apps ecosystem and Telegram analytics.

* [Website](https://www.telemetree.io/)
* [Twitter](https://x.com/telemetree_HQ)
* [Telegram channel](https://t.me/telemetree_en)
* [LinkedIn](https://linkedin.com/company/telemetree)
* [Medium](https://medium.com/@telemetree)
* [Documentation](https://docs.telemetree.io/)
