Blog

Step-by-Step Guide to Get PIWIK PRO Data from API

When you want to get raw data from a report through an API, you can use the API call definition and request information directly from the database.

To fetch report data, follow these steps:

1 – Go to MenuAnalytics.

2 – Navigate to ReportsCustom reports or Goals. 

3 – On the left, select the report you want to work with.

4 – Click the  three-dot icon next to the report you want to use.

5 – Click View API call definition.

6 – Here’s your API endpoint and API query.

7 – Find your Client IDand Client secret

  1. Go Menuyour email address.
  2. On the left, click API keys.
  3. Click Create a key
  4. Name your key and click OK.
  5. Copy Client ID and Client secret. They won’t be available after you close this window.

8 – Call to get your access token.

In the code below, make the following changes:

<account-name>: Use your account name or use the custom account address <account-name>.example.com.  For example, ‘salesdemo’ must be used here because, as you can see in the API endpoint above, our account name is called ‘salesdemo’.

				
					import requests

# Make the POST request to get the access token
auth_response = requests.post(
    "https://salesdemo.piwik.pro/auth/token",
    headers={
        'Content-Type': 'application/json'
    },
    json={
        'grant_type': 'client_credentials',
        'client_id': client_id,
        'client_secret': client_secret
    }
)

# Check the status code and analyse the response
if auth_response.status_code == 200:
    access_token = auth_response.json().get('access_token')
else:
    print(f'Failed to get access token: {auth_response.status_code}')
    print(auth_response.text)
				
			

9 – Call to get data

In the code below, make the following changes:

<website_id>: The website_id is already contained in the copied API query. 

Use the copied API query as payload and the API endpoint as url.

				
					# Endpoint and query for the API request
url = "https://salesdemo.piwik.pro/api/analytics/v1/query/"
payload = {
	"date_from": start_date,
	"date_to": end_date,
	"website_id": "116515f4-9e4c-4ba7-ad97-a71a832db5c9",
	"offset": 0,
	"limit": 10,
	"columns": [
		{
			"column_id": "referrer_type"
		},
		{
			"column_id": "source_medium"
		},
		{
			"column_id": "visitors"
		},
		{
			"column_id": "sessions"
		},
		{
			"column_id": "bounce_rate"
		},
		{
			"column_id": "goal_conversions"
		},
		{
			"column_id": "goal_conversion_rate"
		},
		{
			"transformation_id": "sum",
			"column_id": "goal_revenue"
		}
	]
}

# API request with the access token
response = requests.post(
    url,
    headers={
        'Authorization': f'Bearer {access_token}',
        'Content-Type': 'application/json'
    },
    json=payload
)

# Check the status code and analyse the response
if response.status_code == 200:
    response_data = response.json()
    print('Data received successfully:')
    print(response_data)
else:
    print(f'Failed to get data: {response.status_code}')
    print(response.text)
				
			

More blog articles

Web Analysis

Enhance TikTok Ads tracking with Adjust integration: Streamlining your mobile marketing strategy

With TikTok’s rapid rise as a global marketing platform, advertisers are searching for strategies to enhance ads performance and improve TikTok campaign tracking. One effective ...
Read More →
IT-Wings Gold

Step-by-step guide to creating mobile dashboards

Dashboards are crucial for data analysis and visualization, especially in a mobile world. A well-designed dashboard allows users to quickly grasp important information and make ...
Read More →
Dashboarding

Power BI on mobile devices: dashboards on the go

Let's dive into the features and benefits of using Power BI on mobile devices!
Read More →
Web Analysis

Adjust + Amplitude integration: Bridging Product Analytics and Marketing Insights

App users can come from various channels: paid marketing campaigns, search, social media, and others. Without accurate attribution of App users to traffic sources, it’s ...
Read More →
IT-Wings Gold

Dataflows in Power BI: Advanced Data Preparation Techniques

In today’s data-driven world, the ability to efficiently integrate, transform, and analyze data is crucial for a company’s success. Power BI offers a powerful way ...
Read More →
IT-Wings Gold

Power BI data projects: direct vs. indirect data processing for optimised dashboards

Successfully manage any Power BI data project with efficient solutions for optimum performance.
Read More →