I couldn't find much information in Japanese to get insight data using Google My Business API, so I'll keep it as a memorandum.
https://developers.google.com/my-business/content/prereqs Google API OAuth 2.0 access token & refresh token acquisition procedure February 2017 version
Python(3.6.2)
Google My Business is a free service that allows you to display registered business information on Google Maps and Google Search provided by Google. By registering your own business (restaurants, beauty salons, dentists, etc.) in this service, you can make the business easier for users to see when searching or browsing the map.
It is a function that allows you to acquire, register, update, and delete business information registered in Google My Business. For example, you can reply to reviews posted on business, get access information (insight data) for business, edit shop menus, add photos, etc. systematically using API. I can do it. In this article, I will explain how to get insight data.
This data allows you to check how users found and accessed Google My Business information (restaurants, beauty salons, dentists, etc.). Specifically, you can check the data related to the following items.
・ How the user searched the GMB page ・ Search query ・ Google service used by users to search GMB pages ・ User behavior ・ Number of calls made by users ・ Number of registered photos viewed ・ ・ ・ Etc
To use the Google My Business API, you need to take the following four steps.
After creating a Google account
1-1. Go to Google API Console 1-2. Click "Create Project", enter the project name and click "Create"
2-1. Go to Google API Console Select the project created in 2-2-1 2-3. Check the project ID in [Project ID] 2-4. Fill in and submit the Access Request Form. 2-5. After the submitted content is reviewed, you will receive a follow-up email, so reply
In my case, about two weeks after I submitted it, an English email (the email below) stating "Did you apply for a request to the Google My Business API?" Was sent to the email address you entered in the request form. ..
I replied in simple English, "Yes, I applied.", And a few days later, I received an email to the effect that I was allowed to use the API (the email below).
Please note the following points when filling out the form. (1) You will be asked to enter the email address of the Google account that created the project and the URL of the company's homepage, but the URL and the domain of the email address must match. (2) You will also be asked to enter the company's Google My Business account, so if you do not have one, you need to create one in advance.
3-1. Go to Google API Console. 3-2. Click "Library" from the left menu of the screen 3-3. Search and click Google My Business API 3-4. Click the "Enable" link at the top center of the screen
You will need an access token to make a request to the API. Even if this access token is issued once, it will be invalid after a certain period of time, but it can be reissued if you have a client ID, client secret, and refresh token. Therefore, get this information in order to use the API.
Google API OAuth 2.0 access token & refresh token acquisition procedure February 2017 version
Once you have the credentials, you can get the insight data by accessing the API using Python.
import requests
data = [
('grant_type', 'refresh_token'),
('client_id', '978904852xx-xxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com'),
('client_secret', 'qlDgUPlOIxxxxxxx5-L0yW'),
('refresh_token', '1/9OEydqvV2IExxxxxxxxxxxxxxxxxxx00vWVQ1CCs'),
]
response = requests.post('https://accounts.google.com/o/oauth2/token', data=data)
json = response.json()
access_token = json['access_token']
Get the user account that manages each GMB account.
headers = {
'Authorization': 'Bearer ' + access_token,
}
response = requests.get('https://mybusiness.googleapis.com/v4/accounts', headers=headers)
json = response.json()
account_id = json['accounts'][0]['name'].replace('accounts/','')
Json ['accounts']
is an array of management accounts. Get the account ID of the admin user of the GMB account for which you want to get insight data.Use the account ID obtained in step 2 to get the location name given to each GMB account.
headers = {
'Authorization': 'Bearer ' + access_token,
}
response = requests.get('https://mybusiness.googleapis.com/v4/accounts/'+account_id+'/locations', headers=headers)
json = response.json()
location_name = json['locations'][0]['name']
Json ['locations']
is an array of GMB accounts linked to the management account. Get the location name of the GMB account from which you want to get insight data.Once you have your account ID, location name and access token, you can get insight data with the code below.
headers = {
'Authorization': 'Bearer ' + access_token,
}
#You can set the target period of insight data with startTime and endTime.
data = '{\'locationNames\':[\''+location_name+'\',],\'basicRequest\':{\'metricRequests\':[{\'metric\':\'ALL\'}],\'timeRange\':{\'startTime\':\'2020-10-01T04:00:00.00000000Z\',\'endTime\':\'2020-10-2T04:00:00.00000000Z\',},},}'
response = requests.post('https://mybusiness.googleapis.com/v4/accounts/'+account_id+'/locations:reportInsights', headers=headers, data=data)
json = response.json()
#Direct search
QUERIES_DIRECT = json['locationMetrics'][0]['metricValues'][0]['totalValue']['value']
#Indirect search
QUERIES_INDIRECT = json['locationMetrics'][0]['metricValues'][1]['totalValue']['value']
#Brand search
QUERIES_CHAIN = json['locationMetrics'][0]['metricValues'][2]['totalValue']['value']
#Display via map
VIEWS_MAPS = json['locationMetrics'][0]['metricValues'][3]['totalValue']['value']
#Display via search
VIEWS_SEARCH = json['locationMetrics'][0]['metricValues'][4]['totalValue']['value']
#Access to the website
ACTIONS_WEBSITE = json['locationMetrics'][0]['metricValues'][5]['totalValue']['value']
#Number of users who called
ACTIONS_PHONE = json['locationMetrics'][0]['metricValues'][6]['totalValue']['value']
#Route request
ACTIONS_DRIVING_DIRECTIONS = json['locationMetrics'][0]['metricValues'][7]['totalValue']['value']
#Number of photos viewed(Other companies in the same industry)
PHOTOS_VIEWS_MERCHANT = json['locationMetrics'][0]['metricValues'][8]['totalValue']['value']
#Number of photos viewed(Customer)
PHOTOS_VIEWS_CUSTOMERS = json['locationMetrics'][0]['metricValues'][9]['totalValue']['value']
#Number of photos posted(Other companies in the same industry)
PHOTOS_COUNT_MERCHANT = json['locationMetrics'][0]['metricValues'][10]['totalValue']['value']
#Number of photos posted(Customer)
PHOTOS_COUNT_CUSTOMERS = json['locationMetrics'][0]['metricValues'][11]['totalValue']['value']
headers = {
'Authorization': 'Bearer '+access_token,
}
response = requests.get('https://mybusiness.googleapis.com/v4/'+location_name+'/reviews', headers=headers)
json = response.json()
#Average value of evaluation
averageRating = json['averageRating']
#Total number of reviews
totalReviewCount = json['totalReviewCount']
Recommended Posts