# Querying Analytics Data

**URL:** <https://community.piwik.pro/t/querying-analytics-data/2049>\
**Category:** API & integrations\
**Created:** [April 20, 2023, 11:22am UTC](https://community.piwik.pro/t/querying-analytics-data/2049 "2023-04-20T11:22:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![laurens\_hendrikx](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/laurens_hendrikx/32/1663_2.png) [@laurens\_hendrikx](https://community.piwik.pro/u/laurens_hendrikx)\
**Post date:** [April 20, 2023, 11:22am UTC](https://community.piwik.pro/t/querying-analytics-data/2049/1 "2023-04-20T11:22:40Z")

</div>

I am inquiring about the Analytics API of Piwik Pro.

I am trying to get these metrices/dimensions in a python request:

- sessions (metric - session)

- page\_views (metric - session)

- bounces (metric - session)

- referrer\_type (dimension - session)

- source (dimension - session)

- medium (dimension - session)

- campaign\_name (dimension - session)

- event\_url (dimension - event)

- event\_title (dimension - event)

- time\_on\_page (dimension - event)

1. Why does the /query/ endpoint does not give session\_id etc while the /events/ endpoint does?
2. What is the best way to set up this query where I can also combine event\_url with the correct number of sessions and bounce rate etc.?

Thanks.

---

<div class="post-metadata">

**Author:** ![rmastop](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/rmastop/32/29_2.png) [@rmastop](https://community.piwik.pro/u/rmastop)\
**Post date:** [April 20, 2023, 12:00pm UTC](https://community.piwik.pro/t/querying-analytics-data/2049/2 "2023-04-20T12:00:17Z")

</div>

Hi @laurens_hendrikx,

Using the raw data API ([HTTP API — Piwik PRO Analytics Suite 16.40 documentation](https://developers.piwik.pro/en/latest/custom_reports/http_api/http_api.html#tag/Raw-data)), one can retrieve the individual dimensions and metrics.

1. Session ID is not a dimension you can retrieve vie the /query/ endpoint.

Both raw data session API as well as the raw data events API will return the column session\_id.  
You can use it to blend and calculate the data you require.

1. You can either use the /query/ api to retrieve the required dimensions and metrics and let the calculation be done by Piwik PRO:

```auto
{
	"date_from": "2023-01-01",
	"date_to": "2023-12-31",
	"website_id": "dae17aa5-85fe-412b-9f2e-4123bd52cd97",
	"offset": 0,
	"limit": 10,
	"columns": [
		{
			"column_id": "referrer_type"
		},
		{
			"column_id": "source"
		},
		{
			"column_id": "medium"
		},
		{
			"column_id": "campaign_name"
		},
		{
			"column_id": "session_entry_url"
		},
		{
			"column_id": "session_entry_title"
		},
		{
			"column_id": "event_url"
		},
		{
			"column_id": "event_title"
		},
		{
			"column_id": "time_on_page"
		},
		{
			"column_id": "is_bounce"
		},
		{
			"column_id": "sessions"
		}
	],
	"order_by": [
		[
			10,
			"desc"
		]
	],
	"filters": {
		"operator": "and",
		"conditions": [
			{
				"operator": "or",
				"conditions": [
					{
						"column_id": "event_type",
						"condition": {
							"operator": "eq",
							"value": 1
						}
					}
				]
			}
		]
	},
	"metric_filters": null
}

```

If you need to filter based on campaign data only, you can use this:

```auto
{
	"date_from": "2023-01-01",
	"date_to": "2023-12-31",
	"website_id": "dae17aa5-85fe-412b-9f2e-4123bd52cd97",
	"offset": 0,
	"limit": 10,
	"columns": [
		{
			"column_id": "referrer_type"
		},
		{
			"column_id": "source"
		},
		{
			"column_id": "medium"
		},
		{
			"column_id": "campaign_name"
		},
		{
			"column_id": "session_entry_url"
		},
		{
			"column_id": "session_entry_title"
		},
		{
			"column_id": "event_url"
		},
		{
			"column_id": "event_title"
		},
		{
			"column_id": "time_on_page"
		},
		{
			"column_id": "is_bounce"
		},
		{
			"column_id": "sessions"
		}
	],
	"order_by": [
		[
			10,
			"desc"
		]
	],
	"filters": {
		"operator": "and",
		"conditions": [
			{
				"operator": "or",
				"conditions": [
					{
						"column_id": "event_type",
						"condition": {
							"operator": "eq",
							"value": 1
						}
					}
				]
			},
			{
				"operator": "or",
				"conditions": [
					{
						"column_id": "referrer_type",
						"condition": {
							"operator": "eq",
							"value": 6
						}
					}
				]
			}
		]
	},
	"metric_filters": null
}

```

You can basically build the report in Piwik PRO Analytics and check the API definition in the advanced section (as seen in the documentation: [HTTP API — Piwik PRO Analytics Suite 16.40 documentation](https://developers.piwik.pro/en/latest/custom_reports/http_api/http_api.html#tag/Queries)):

Keep in mind the offset and limit, in case you need to retrieve a lot of rows.

Sometimes it’s better to call the API multiple times (for instance because you want to know the bounce rate per campaign, instead of per campaign, per source, per medium, per entry page URL, etc…)

If you want to calculate it yourself, you can use the Raw data API and retrieve all the required dimensions and metrics, as documented here:

> **[HTTP API](https://developers.piwik.pro/en/latest/custom_reports/http_api/http_api.html#tag/Raw-data)**

It will automatically return the session\_id as part of the returned dataset, for both the events as well as the sessions API, so you can use it to blend the dataset.

Does this help?

Kind regards,  
Richard

---

<div class="post-metadata">

**Author:** ![laurens\_hendrikx](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/laurens_hendrikx/32/1663_2.png) [@laurens\_hendrikx](https://community.piwik.pro/u/laurens_hendrikx)\
**Post date:** [April 20, 2023, 6:51pm UTC](https://community.piwik.pro/t/querying-analytics-data/2049/4 "2023-04-20T18:51:35Z")

</div>

Hi, but when I am trying to use the sessions API with the endpoint /sessions/, I get this error:  
{‘errors’: [{‘status’: ‘400’, ‘detail’: ‘Metric may not be used here.’, ‘source’: {‘parameter’: ‘columns.1.column\_id’}}, {‘status’: ‘400’, ‘detail’: ‘Metric may not be used here.’, ‘source’: {‘parameter’: ‘columns.2.column\_id’}}]}

For the following query:  
query = {  
“date\_from”: date\_from,  
“date\_to”: date\_to,  
“website\_id”: website\_id,  
“offset”: 0,  
“limit”: 100000,  
“columns”: [{  
“column\_id”: “referrer\_type”}, {  
“column\_id”: “sessions”}, {  
“column\_id”: “bounces”  
}  
],  
“order\_by”: [  
[0,“asc”]  
]  
}

I cannot figure out how to get these metrics and dimensions in the /sessions/ endpoint?

---

<div class="post-metadata">

**Author:** ![rmastop](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/rmastop/32/29_2.png) [@rmastop](https://community.piwik.pro/u/rmastop)\
**Post date:** [April 25, 2023, 10:35am UTC](https://community.piwik.pro/t/querying-analytics-data/2049/5 "2023-04-25T10:35:54Z")

</div>

Hi @laurens_hendrikx,

The sessions API provides you with raw session data, meaning you would need to calculate it based on the dimensions you retrieve from the API;

- session\_id (will be available by default)
- is\_bounce
- referrer\_type

Based on this info you can calculate the amount of sessions (count unique session\_id)  
Split them by bounce / not bounce ( 0 / 1 )  
split them by referrer type, using the referrer\_type.json (As seen in [Columns — Piwik PRO Analytics Suite 16.40 documentation](https://developers.piwik.pro/en/latest/custom_reports/columns.html)

```auto
[
    [
        1,
        "Direct entry"
    ],
    [
        2,
        "Search engine"
    ],
    [
        3,
        "Website"
    ],
    [
        6,
        "Campaign"
    ],
    [
        7,
        "Social"
    ]
]

```

)

Does it help?
