Hi Markus!
Itās not possible in the Administration module UI but itās possible to automate it using the Apps API endpoint.
Some examples I can share here (written in Python):
- I prefer storing API credentials in a
.json
file. This way I can usually have a test file and āproductionā file in case Iām test running a complex script.
{
"client_id": "client_id",
"client_secret": "client_secret",
"instance_url": "https://example.piwik.pro"
}
def get_credentials_from_file(filename):
with open(filename, "r") as json_file:
credentials = json.load(json_file)
return credentials
- Getting the auth token.
def get_auth_token(credentials):
auth_body = {"grant_type": "client_credentials", "client_id": credentials["client_id"], "client_secret": credentials["client_secret"]}
return requests.post(credentials["instance_url"] + '/auth/token', data=auth_body).json()["access_token"]
- Creating the site/app - this function will create an
output.csv
file that contains website name and website UUID columns. Itās not necessary, just a nice thing to have.
def create_new_app(instance_url, token, website_name, website_urls):
payload = json.dumps({
"data": {
"type": "ppms/app",
"attributes": {
"appType": "web",
"name": website_name,
"urls": website_urls
}
}
})
try:
response = requests.post(instance_url + '/api/apps/v2', headers={"Authorization": 'Bearer ' + token, "Content-Type": "application/vnd.api+json"}, data=payload)
except requests.exceptions.HTTPError as e:
if e.response.status_code == 401:
print("Auth token is no longer valid.")
token = get_auth_token()
response = requests.post(instance_url + '/api/apps/v2', headers={"Authorization": 'Bearer ' + token, "Content-Type": "application/vnd.api+json"}, data=payload)
else:
print("Request error occured.")
raise
response_content = json.loads(response.content)['data']
site_id = response_content['id']
site_name = response_content['attributes']['name']
csv_row = [site_name, site_id]
with open(r'output.csv', 'a') as f:
writer = csv.writer(f)
writer.writerow(csv_row)
return website_name + " finished with code " + str(response.status_code)
- When it comes to the data source - there are multiple choices. I usually go with CSV files, so it should look something like this.
with open('filename.csv', newline='') as f:
csv_file = csv.reader(f, delimiter=',', quotechar='|')
next(csv_file, None) #skip headers
csv_data = [row for row in csv_file]
for row in csv_data:
# row[0] is the website name and row[1] is an array of website URLs (strings)
print(create_new_app(credentials["instance_url"], token, row[0], row[1]))
time.sleep(1)