Marco Baez
Marco Baez

Reputation: 1

How to query a Google My Business API for Insights

I have created a report where I want to also include all the insights of a Google My Business account.

I have already been approved and have access to the GMB API with no problem. The only thing is now that I have full access, how do I successfully query it so I can get insight information? I have access to a team that works with PHP or Python so I wanted to see what I should give them so that they can start querying successfully. Can anyone help?

Upvotes: 0

Views: 1111

Answers (2)

Shila Mosammami
Shila Mosammami

Reputation: 1077

I work with java to do the same stuff.

Mine is something like this:

ReportLocationInsightsRequest content = new ReportLocationInsightsRequest();
    content.setFactory(JSON_FACTORY);
    BasicMetricsRequest basicRequest = new BasicMetricsRequest();
content.setLocationNames("your locationName as a list");
List<MetricRequest> metricRequests= new ArrayList<MetricRequest>();
    MetricRequest metricR=new MetricRequest();
    String metric="ALL";
    metricR.setMetric(metric);
    metricRequests.add(metricR);
    TimeRange timeRange =new TimeRange();
    timeRange.setStartTime("Desired startTime");
    timeRange.setEndTime("Desired endTime");
    basicRequest.setTimeRange(timeRange );
    content.setBasicRequest(basicRequest );
    try {
        MyBusiness.Accounts.Locations.ReportInsights locationReportInsight= 
                mybusiness.accounts().locations().reportInsights(accountName, content);
        ReportLocationInsightsResponse response= locationReportInsight.execute();
        System.out.println("response is = "+ response.toPrettyString());
}catch(Exception e) {
System.out.println(e);
}

Upvotes: 0

Sudheer m
Sudheer m

Reputation: 86

Download php client library from here

Here is the sample function to get location insights

Parameters required:

  • locationNames should be provided as input
  • startTime and endTime max difference should be 18 months (2020-01-01T15:01:23Z,2021-01-01T15:01:23Z)
   public function getLocationInsights($accountName,$parameters){ 

        // Replace getClientService, with method having accesstoken
        $service = $this->getClientService();
        $insightReqObj = new Google_Service_MyBusiness_ReportLocationInsightsRequest();
        $locationNames = $parameters['locationNames'];

        // Atleast one location mandatory
        if($locationNames && is_array($locationNames) && count($locationNames) <=10){
            $insightReqObj->setLocationNames($locationNames);
        }
        
        $basicReqObj = new Google_Service_MyBusiness_BasicMetricsRequest();
        // datetime range is mandatory
        // TODO :: validate to not allow more than 18 months difference
        $timeRangObj = new Google_Service_MyBusiness_TimeRange();
        $timeRangObj->setStartTime($parameters['startTime']);
        $timeRangObj->setEndTime($parameters['endTime']);

        $metricReqObj = new Google_Service_MyBusiness_MetricRequest();
        $metricReqObj->setMetric('ALL');

        $basicReqObj->setMetricRequests(array($metricReqObj));
        $basicReqObj->setTimeRange($timeRangObj);
        $insightReqObj->setBasicRequest($basicReqObj);
        $allInsights = $service->accounts_locations->reportInsights($accountName,$insightReqObj);
       
        return $allInsights;
    }

Upvotes: 2

Related Questions