# Query Custom Analytics — YouTube Analytics

> Execute a custom analytics query using specified metrics, dimensions, filters, and date ranges. Requires query parameters to configure. See the documentation.

- Key: `youtube_analytics_api-query-custom-analytics`
- Type: Action (Read-only)
- Version: 0.0.5
- App: YouTube Analytics (`youtube_analytics_api`) — https://pipedream.com/apps/youtube-analytics-api.md
- This page (HTML): https://pipedream.com/apps/youtube-analytics-api/actions/query-custom-analytics
- Hints: read-only · open-world
- Source: https://github.com/PipedreamHQ/pipedream/blob/master/components/youtube_analytics_api/actions/query-custom-analytics/query-custom-analytics.mjs

## Description

Execute a custom analytics query using specified metrics, dimensions, filters, and date ranges. Requires query parameters to configure. [See the documentation](https://developers.google.com/youtube/analytics/reference/reports/query).

## Props

| Prop | Type | Required | Description |
|---|---|---|---|
| `startDate` | `string` | Yes | The start date for fetching YouTube Analytics data. The value should be in YYYY-MM-DD format. |
| `endDate` | `string` | Yes | The end date for fetching YouTube Analytics data. The value should be in YYYY-MM-DD format. The API response contains data up until the last day for which all metrics in the query are available at the time of the query. So, for example, if the request specifies an end date of July 5, 2017, and values for all of the requested metrics are only available through July 3, 2017, that will be the last date for which data is included in the response. (That is true even if data for some of the requested metrics is available for July 4, 2017.) |
| `dimensions` | `string[]` | No | A list of YouTube Analytics dimensions, such as video or ageGroup, gender. See the documentation for channel reports or content owner reports for a list of the reports that you can retrieve and the dimensions used for those reports. (The Dimensions document contains definitions for all of the dimensions.). |
| `sort` | `string[]` | No | A list of dimensions or metrics that determine the sort order for YouTube Analytics data. By default the sort order is ascending. The - prefix causes descending sort order. Eg. -views. |
| `maxResults` | `integer` | No | The maximum number of rows to include in the response. |
| `idType` | `string` | Yes | The type of ID to use for the query. This can be either My Channel, Channel ID, or Content Owner. |
| `ids` | `string` | No | Required when ID Type is Channel ID (e.g. UC_x5XG1OV2P6uZZ5FSM9Ttw) or Content Owner (e.g. MyContentOwnerName or contentOwner@example.com), and unused when ID Type is My Channel. |
| `metrics` | `string[]` | Yes | Metrics, such as views or likes, dislikes. Use the Get Channel Report Metrics action to get a list of the metrics available in each report. See the documentation for channel reports or content owner reports for a list of the reports that you can retrieve and the metrics available in each report. (The Metrics document contains definitions for all of the metrics.). |
| `filters` | `object` | No | A list of filters that should be applied when retrieving YouTube Analytics data. The documentation for channel reports and content owner reports identifies the dimensions that can be used to filter each report, and the Dimensions document defines those dimensions. If a request uses multiple filters the returned result table will satisfy both filters. For example, a filters parameter value of {"video":"dMH0bHeiRNg","country":"IT"} restricts the result set to include data for the given video in Italy. Specifying multiple values for a filter The API supports the ability to specify multiple values for the video, playlist, and channel filters. To do so, specify a separated list of the video, playlist, or channel IDs for which the API response should be filtered. For example, a filters parameter value of {"video":"pd1FJh59zxQ,Zhawgd0REhA","country":"IT"} restricts the result set to include data for the given videos in Italy. The parameter value can specify up to 500 IDs. For more details on the filters parameter, see the filters parameter in Parameters section. |

## Run it

**MCP**

```ts
import { Client } from "@modelcontextprotocol/sdk/client/index.js"
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"
import { PipedreamClient } from "@pipedream/sdk"

const pd = new PipedreamClient({
  projectId: process.env.PIPEDREAM_PROJECT_ID!,
  clientId: process.env.PIPEDREAM_CLIENT_ID!,
  clientSecret: process.env.PIPEDREAM_CLIENT_SECRET!,
  projectEnvironment: "production",
})

const accessToken = await pd.rawAccessToken

const transport = new StreamableHTTPClientTransport(
  new URL("https://remote.mcp.pipedream.net/v3"),
  {
    requestInit: {
      headers: {
        Authorization: `Bearer ${accessToken}`,
        "x-pd-project-id": process.env.PIPEDREAM_PROJECT_ID!,
        "x-pd-environment": "production",
        "x-pd-external-user-id": "{external_user_id}", // any stable ID for this user in your system
        "x-pd-app-slug": "youtube_analytics_api",
      },
    },
  },
)

const mcp = new Client({ name: "my-agent", version: "1.0.0" })
await mcp.connect(transport)

const { tools } = await mcp.listTools()

// listTools() hands your model this tool's input schema, so it can
// fill the arguments itself:
const result = await mcp.callTool({
  name: "youtube_analytics_api-query-custom-analytics",
  arguments: {
    startDate: "Start Date",
    endDate: "End Date",
  },
})
```

**TypeScript**

```ts
import { PipedreamClient } from "@pipedream/sdk"

const pd = new PipedreamClient({
  projectId: process.env.PIPEDREAM_PROJECT_ID!,
  clientId: process.env.PIPEDREAM_CLIENT_ID!,
  clientSecret: process.env.PIPEDREAM_CLIENT_SECRET!,
  projectEnvironment: "production",
})

const result = await pd.actions.run({
  id: "youtube_analytics_api-query-custom-analytics",
  externalUserId: "{external_user_id}", // any stable ID for this user in your system
  configuredProps: {
    youtube_analytics_api: { authProvisionId: "apn_xxxxxxx" },
    startDate: "Start Date",
    endDate: "End Date",
  },
})

console.log(result)
```

**cURL**

```bash
curl -X POST https://api.pipedream.com/v1/connect/{project_id}/actions/run \
  -H "Content-Type: application/json" \
  -H "X-PD-Environment: production" \
  -H "Authorization: Bearer {access_token}" \
  -d '{
    "external_user_id": "{external_user_id}",
    "id": "youtube_analytics_api-query-custom-analytics",
    "configured_props": {
      "youtube_analytics_api": { "authProvisionId": "apn_xxxxxxx" },
      "startDate": "Start Date",
      "endDate": "End Date"
    }
  }'
```

---

- App: https://pipedream.com/apps/youtube-analytics-api.md · All apps: https://pipedream.com/apps
