My Business
1.8K members online now
1.8K members online now
For developers who are using the Google My Business API to manage locations
Guide Me
star_border
Reply

PHP Code Example Insights with MyBusiness.php v3.2

[ Edited ]
Follower ✭ ☆ ☆
# 1
Follower ✭ ☆ ☆

Is there someone who can share a working example for retrieving insights with MyBusiness.php V3.2 

Thanks is advance.

 

Or mayby someone can point what i am doing wrong. This code keeps Trowing: "Argument 2 passed to Google_Service_MyBusiness_AccountsLocations_Resource::reportInsights() must be an instance of Google_Service_MyBusiness_ReportLocationInsightsRequest, none given, called in mysource on line 21 and defined in MyBusiness.php on line 934" :

 

require_once 'vendor/autoload.php'; // or wherever autoload.php is located
require_once './MyBusiness.php';

define('APPLICATION_NAME', 'User Query - Google My Business API');
define('CREDENTIALS_PATH', __DIR__ .'/config/authorization_token.json');
define('CLIENT_SECRET_PATH', __DIR__ .'/config/client_secrets.json'); // included the o auth client json here. downloaded from google api console


$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setAuthConfigFile(CLIENT_SECRET_PATH);
$client->addScope("https://www.googleapis.com/auth/plus.business.manage");
$client->setRedirectUri($redirect_uri);
$client->setAccessType("offline");
$client->setApprovalPrompt("force");
$mybusinessService = new Google_Service_Mybusiness($client);
$credentialsPath = CREDENTIALS_PATH;

$reportInsightsArr= array('locationNames' => 'accounts/xxx/locations/yyy' );
$test=$mybusinessService->accounts_locations->reportInsights($reportInsightsArr);
print "<pre>";
print_r($test);

1 Expert replyverified_user

Re: PHP Code Example Insights with MyBusiness.php v3.2

Google Employee
# 2
Google Employee

Hi @Paul v,


Please follow the example code snippet below for retrieving Location Insights with the PHP client library:

 

// Retrieves basic insights for a given list of locations.
$reportLocationInsightsRequest = new Google_Service_MyBusiness_ReportLocationInsightsRequest(); $basicRequest = new Google_Service_MyBusiness_BasicMetricsRequest(); $metricRequests = new Google_Service_MyBusiness_MetricRequest(); $metricRequests->setMetric("ALL"); $basicRequest->setMetricRequests($metricRequests); $timeRange = new Google_Service_MyBusiness_TimeRange(); $timeRange->setStartTime("2016-10-12T01:01:23.045123456Z"); $timeRange->setEndTime("2017-01-10T23:59:59.045123456Z"); $basicRequest->setTimeRange($timeRange); $reportLocationInsightsRequest->setBasicRequest($basicRequest); $reportLocationInsightsRequest->setLocationNames(array($location->name)); $reportLocationInsightsResponse = $locations->reportInsights($account->name, $reportLocationInsightsRequest);
$locationMetrics = $reportLocationInsightsResponse->getLocationMetrics(); $locationMetricsArray = array(); $locationMetricsArray['locationMetrics']['locationName'] = array($locationMetrics[0]->getLocationName()); $locationMetricsArray['locationMetrics']['timeZone'] = array($locationMetrics[0]->getTimeZone()); $metricValuesArray = array();
foreach ($locationMetrics[0]->getMetricValues() as $value) { $metricValuesItem = array(); $metricValuesItem['metric'] = $value['metric']; $metricValuesItem['totalValue']['timeDimension']['timeRange']['endTime'] = $value['totalValue']['timeDimension']['timeRange']['endTime']; $metricValuesItem['totalValue']['timeDimension']['timeRange']['startTime'] = $value['totalValue']['timeDimension']['timeRange']['startTime']; $metricValuesItem['totalValue']['metricOption'] = $value['totalValue']['metricOption']; $metricValuesItem['totalValue']['value'] = $value['totalValue']['value']; $metricValuesArray[] = $metricValuesItem; } $locationMetricsArray['locationMetrics']['metricValues'] = $metricValuesArray; print json_encode($locationMetricsArray, JSON_PRETTY_PRINT) . "\n";

 

// Retrieves driving directions metrics for a given list of locations.
$reportLocationInsightsRequest = new Google_Service_MyBusiness_ReportLocationInsightsRequest(); $drivingDirectionsRequest = new Google_Service_MyBusiness_DrivingDirectionMetricsRequest(); $drivingDirectionsRequest->setNumDays("NINETY"); $reportLocationInsightsRequest->setDrivingDirectionsRequest($drivingDirectionsRequest); $reportLocationInsightsRequest->setLocationNames(array($location->name)); $reportLocationInsightsResponse = $locations->reportInsights($account->name, $reportLocationInsightsRequest); $locationDrivingDirectionMetrics = $reportLocationInsightsResponse->getLocationDrivingDirectionMetrics(); $locationDrivingDirectionMetricsArray = array(); $locationDrivingDirectionMetricsArray['locationDrivingDirectionMetrics']['locationName'] = array($locationDrivingDirectionMetrics[0]->getLocationName()); $locationDrivingDirectionMetricsArray['locationDrivingDirectionMetrics']['timeZone'] = array($locationDrivingDirectionMetrics[0]->getTimeZone()); $topDirectionSourcesArray = array();
foreach ($locationDrivingDirectionMetrics[0]->getTopDirectionSources() as $value) { $topDirectionSourcesItem = array(); $topDirectionSourcesItem['dayCount'] = $value['dayCount']; $topDirectionSourcesItem['regionCounts']['latlng']['latitude'] = $value['regionCounts']['latlng']['latitude']; $topDirectionSourcesItem['regionCounts']['latlng']['longitude'] = $value['regionCounts']['latlng']['longitude']; $topDirectionSourcesItem['regionCounts']['label'] = $value['regionCounts']['label']; $topDirectionSourcesItem['regionCounts']['count'] = $value['regionCounts']['count']; $topDirectionSourcesArray[] = $topDirectionSourcesItem; } $locationDrivingDirectionMetricsArray['locationDrivingDirectionMetrics']['topDirectionSources'] = $topDirectionSourcesArray; print json_encode($locationDrivingDirectionMetricsArray, JSON_PRETTY_PRINT) . "\n";

 

I hope this helps!

 

Thanks,

The Google My Business API team 

Re: PHP Code Example Insights with MyBusiness.php v3.2

Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭

I tried implementing the same but came across with the issue Class 'Google_Service' not found in Mybusiness.php on line 33

Please help

PHP Code Example Insights with MyBusiness.php v3.2

Visitor ✭ ✭ ✭
# 4
Visitor ✭ ✭ ✭

Got Error as  Class 'Google_Service' not found in Mybusiness.php on line 33, Please help

 

PHP Code Example Insights with MyBusiness.php v3.2

Follower ✭ ☆ ☆
# 5
Follower ✭ ☆ ☆

Thanks Shalini, this works great!

PHP Code Example Insights with MyBusiness.php v3.2

Follower ✭ ☆ ☆
# 6
Follower ✭ ☆ ☆

Hi Shali,

 

Could you indicate what i am doing wrong? Adding $metricRequests->setOptions("AGGREGATED_DAILY");
will not aggregate per day? but will give the default AGGREGATED_TOTAL.

$reportLocationInsightsRequest = new Google_Service_MyBusiness_ReportLocationInsightsRequest();
$basicRequest = new Google_Service_MyBusiness_BasicMetricsRequest();
$metricRequests = new Google_Service_MyBusiness_MetricRequest();
$metricRequests->setMetric("ALL");
$metricRequests->setOptions("AGGREGATED_DAILY");
$basicRequest->setMetricRequests($metricRequests); $timeRange = new Google_Service_MyBusiness_TimeRange(); $timeRange->setStartTime("2016-10-12T01:01:23.045123456Z"); $timeRange->setEndTime("2017-01-10T23:59:59.045123456Z"); $basicRequest->setTimeRange($timeRange);

 

Re: PHP Code Example Insights with MyBusiness.php v3.2

Google Employee
# 7
Google Employee

Hi @Paul v,

 

Could you please confirm if you are getting any error when fetching AGGREGATED_DAILY insights for your location?

 

Thanks,

The Google My Business API team

PHP Code Example Insights with MyBusiness.php v3.2

Follower ✭ ☆ ☆
# 8
Follower ✭ ☆ ☆

Hi Shalini,


There is NO Error.  
When i put in a Wrongly Value ( $metricRequests->setOptions("WRONG_VALUE");  It does. (400 Invalid Argument.) )

The Request object is like this:

            [metricRequests] => Google_Service_MyBusiness_MetricRequest Object
                (
                    [collection_key:protected] => options
                    [internal_gapi_mappings:protected] => Array
                        (
                        )

                    [metric] => ALL
                    [options] => AGGREGATED_DAILY
                    [modelData:protected] => Array
                        (
                        )

                    [processed:protected] => Array
                        (
                        )

                )

But the Output is like this:

        "metricValues": [
            {
                "metric": "QUERIES_DIRECT",
                "totalValue": {
                    "timeDimension": {
                        "timeRange": {
                            "endTime": "2017-03-31T23:59:59.045123456Z",
                            "startTime": "2017-03-01T23:59:59.045123456Z"
                        }
                    },
                    "metricOption": "AGGREGATED_TOTAL",
                    "value": "140"
                }
            },

Thanks in advance,

Paul

Re: PHP Code Example Insights with MyBusiness.php v3.2

Google Employee
# 9
Google Employee

Hi @Paul v,

 

Thanks for the information. We are looking into this issue and will get back to you soon.

 

Thanks,

The Google My Business API team

PHP Code Example Insights with MyBusiness.php v3.2

Follower ✭ ☆ ☆
# 10
Follower ✭ ☆ ☆

Hi Shalini,


Any news on this subject? Hope that it can be resolved soon. 

Thanks,

 

Paul