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

# Transitioning from Open Analytics

> Plan your move from Open Analytics: save your queries, download your reports, migrate reports to MoEngage Analytics, and set up data exports.

If you plan to move from Open Analytics to your own tool or to MoEngage Analytics, plan these steps:

1. **Save and export your queries.** Copy or export the SQL of each query you want to keep. For more information, refer to [Save Your Queries](#save-your-queries).
2. **Download your reports.** Download the results of the queries you need. For more information, refer to [Download Query Results](#download-query-results).
3. **Migrate reports to MoEngage Analytics.** Rebuild the reports that MoEngage Analytics supports in [custom dashboards](https://moengage.com/docs/user-guide/analyze/dashboards/custom-dashboards.md).
4. **Set up data exports.** If you don't use them already, set up [MoEngage Data Exports](https://moengage.com/docs/user-guide/data/exports/events/overview-events-exports.md) to send your MoEngage data to your own warehouse or data tools. Use them to run these workloads on a recurring basis.

> [!NOTE]
>
> To export historical events, create a support ticket. This export can incur charges and needs admin approval. It can take a few weeks, depending on the number of events and their date range.

Open Analytics is built on Redash, so the screens in this guide are Redash screens. You can export two types of content from it:

* **Queries:** the SQL text, for backups, code reviews, version control, or migrations.
* **Query results:** the data a query returns, as CSV, TSV, Excel, or JSON, for reports, spreadsheets, or other tools.

## Choose an Export Method

| Goal | Method | Authentication | Skill level |
| - | - | - | - |
| Save the SQL of one query | [Copy it from the editor](#copy-the-sql-from-the-editor) | Open Analytics login | Anyone |
| Save the SQL of every query | [Export with redash-toolbelt](#export-all-queries-with-redash-toolbelt) | User API key | Technical |
| Download the results of one query once | [Download from the console](#download-results-from-the-console) | Open Analytics login | Anyone |
| Read the latest results in a script | [Results API](#get-results-from-the-api) | Query API key | Light technical |

## Before You Start

The API and command-line methods need a query ID, an API key, or both.

* **Query ID:** Open the query and copy the number at the end of the browser URL. For example, in `https://<OPEN_ANALYTICS_URL>/queries/4821`, the query ID is `4821`.
* **API key:** Use the key type that matches your task.

| Key type | Where to find it | Access | Use it for |
| - | - | - | - |
| Query API key | On the query page, click the horizontal ellipsis (**...**) next to **Edit Source**, and click **Show API Key**. | The results of that one query only. | Sharing result URLs and scripts that only read results. |
| User API key | Go to **Profile** > **API Key**. | Everything your user account can access. | Fetching the SQL of queries and exporting all your queries. |

## Save Your Queries

You can copy the SQL of one query from the editor, or export every query you can access as `.sql` files. Copying works for a few queries. For more than a few, the bulk export is faster and keeps each query's name with its SQL.

### Copy the SQL From the Editor

**1. Open the Query**

In the top navigation, click **Queries** and select the query from the list or from **Favorite Queries**.

**2. Show the SQL**

Click **Edit Source**. The SQL editor opens above the results.

**3. Copy the SQL**

Click in the editor, press `Cmd+A` (macOS) or `Ctrl+A` (Windows) to select the full query, and copy it. Paste it into a text file or your code repository.

![Open a query from the Queries menu, click Edit Source, and select the SQL in the editor.](https://mintcdn.com/moengage/lCqGMxntU4uj6TSM/images/analyze/open-analytics/copy-query-sql.gif?s=7a800d7b26e8185c37a845471652d1f2)

To fetch the SQL with a script instead, send a `GET` request with your user API key. The response is a JSON object that describes the query, and the SQL is in its `query` field.

```bash Get One Query theme={null}
curl -H "Authorization: Key <USER_API_KEY>" \
  "https://<OPEN_ANALYTICS_URL>/api/queries/<QUERY_ID>"
```

To save only the SQL to a file, filter the response with [jq](https://jqlang.github.io/jq/):

```bash Save One Query as SQL theme={null}
curl -s -H "Authorization: Key <USER_API_KEY>" \
  "https://<OPEN_ANALYTICS_URL>/api/queries/<QUERY_ID>" \
  | jq -r '.query' > query_<QUERY_ID>.sql
```

### Export All Queries With redash-toolbelt

The `export-queries` command in the official [redash-toolbelt](https://github.com/getredash/redash-toolbelt) Python package saves each query you can access as a `query_<QUERY_ID>.sql` file in the current folder. Each file starts with a comment that lists the query name, data source, author, and last update time.

**1. Install redash-toolbelt**

```bash Install theme={null}
pip install redash-toolbelt
```

**2. Set Your User API Key**

```bash Set the API Key theme={null}
export REDASH_API_KEY="<USER_API_KEY>"
```

If you skip this step, the command prompts you for the key.

**3. Run the Export**

```bash Export All Queries theme={null}
export-queries "https://<OPEN_ANALYTICS_URL>"
```

## Download Query Results

You can download the results of a query as a file from the console, or read them from a URL with the query API key. Use the console for a one-time file. Use the API when a script needs the latest results.

### Download Results From the Console

**1. Run the Query**

Open the query and click **Execute** so the results are current.

**2. Open the Results Menu**

Below the results table, click the vertical ellipsis (**⋮**) next to **Edit Visualization**.

**3. Download the File**

Click **Download as CSV File**, **Download as TSV File**, or **Download as Excel File**.

![The results menu below the query results with options to download as CSV, TSV, or Excel.](https://mintcdn.com/moengage/lCqGMxntU4uj6TSM/images/analyze/open-analytics/redash-download-dataset.png?fit=max\&auto=format\&n=lCqGMxntU4uj6TSM\&q=85\&s=c957f45c83e98153c25f81ba9e246083)

To download from a dashboard, open the widget menu and select the download option. You get the same file for the query behind that widget.

> [!TIP]
>
> The download contains the full cached result, not only the rows on screen. If a query returns millions of rows, aggregate the data or add a `LIMIT` before you download. Very large downloads can time out.

### Get Results From the API

Each saved query has its own query API key. With it, a script can read the query's results over HTTPS without logging in.

**1. Copy the Query API Key**

Open the query, click the horizontal ellipsis (**...**) next to **Edit Source**, and click **Show API Key**. The dialog shows the key and example URLs for CSV and JSON results.

**2. Build the Results URL**

Add the query ID and key to the URL. To get JSON or Excel instead of CSV, change `.csv` to `.json` or `.xlsx`.

```text Results URL theme={null}
https://<OPEN_ANALYTICS_URL>/api/queries/<QUERY_ID>/results.csv?api_key=<QUERY_API_KEY>
```

![The query menu with Show API Key selected and the dialog showing the API key and result URLs.](https://mintcdn.com/moengage/lCqGMxntU4uj6TSM/images/analyze/open-analytics/redash-show-api-key.png?fit=max\&auto=format\&n=lCqGMxntU4uj6TSM\&q=85\&s=ff4b16d355a42a16bce64e30bdbd95c3)

> [!WARNING]
>
> Anyone with this URL can download the results without logging in. Treat the URL like a password, and don't commit it to a code repository.

The results URL works as follows:

* **It returns the latest result.** If you add a result ID to the path, for example `/results/<RESULT_ID>.csv`, the URL stays pinned to that one result and doesn't update.
* **It returns cached results only.** The URL doesn't run the query. If the query has never run, you get a `No cached result found` error. Run the query or set a refresh schedule first.
* **It doesn't work for queries with parameters.** For a query that uses parameters, such as `{{limit}}`, you get a `No cached result found` error. To get these results, send a `POST` request to `/api/queries/<QUERY_ID>/results` with your user API key. For details, see the [Redash API reference](https://redash.io/help/user-guide/integrations-and-api/api).
* **It can fail on very large results.** Results of about 200 MB or more can fail with a `502 Bad Gateway` error. Filter or aggregate the data, or split the query into smaller date ranges.

You can call the URL from any tool that reads files over HTTPS:

```bash cURL theme={null}
curl -L "https://<OPEN_ANALYTICS_URL>/api/queries/<QUERY_ID>/results.csv?api_key=<QUERY_API_KEY>" \
  -o results.csv
```

```python Python theme={null}
import pandas as pd

url = "https://<OPEN_ANALYTICS_URL>/api/queries/<QUERY_ID>/results.csv?api_key=<QUERY_API_KEY>"
df = pd.read_csv(url)
print(df.head())
```

## Security Practices

* **Use the narrowest key.** Use a query API key to share or automate results. Use your user API key only in scripts you control.
* **Keep keys out of code.** Store API keys in environment variables or a secrets manager. Don't put keys or results URLs in source code or Git.
* **Protect customer data.** Exported files can contain personally identifiable information (PII). Check who can open a file before you share it.
* **Back up on a schedule.** Run `export-queries` on a schedule into a private Git repository. This gives you a version history and a way to recover a query that someone changes or deletes.

## Troubleshooting

| Problem | Cause | Fix |
| - | - | - |
| `No cached result found` | The query has never run, or it uses parameters. | Run the query or set a refresh schedule. For a query with parameters, send a `POST` request to `/api/queries/<QUERY_ID>/results` with your user API key. |
| `401 Unauthorized` or `403 Forbidden` | The key type is wrong, or it's sent the wrong way. | For `/api/queries` endpoints, use a user API key in the `Authorization: Key <USER_API_KEY>` header. Query API keys work only on results URLs. |
| Only some queries exported | Your user API key exports only the queries you can access. | Export the remaining queries with the user API key of someone who can access them. |
| Results are stale | The URL has a result ID, or the query hasn't run recently. | Remove the result ID so the path ends with `/results.csv`, and set a refresh schedule. |
| `502 Bad Gateway` | The result is too large. | Filter or aggregate the data, or split the query into smaller date ranges. |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.