The Google Reporting API documentation does not give an example of how to use the PHP client to access UserActivity information given a specific UserId. There are several questions on StackOverflow about this, but I wanted to document my solution here in case it helps anyone.
I had to dive into the PHP client to figure this out.
error_reporting(E_ALL); ini_set('display_errors', 1); // Load the Google API PHP Client Library. require_once __DIR__ . '/vendor/autoload.php'; session_start(); $client = new Google_Client(); $client->setAuthConfig(__DIR__ . '/your-json-key-here.json'); $client->addScope(Google_Service_Analytics::ANALYTICS_READONLY); // Create an authorized analytics service object. $analytics = new Google_Service_AnalyticsReporting($client); // Call the Analytics Reporting API V4. $response = getReport($analytics); function getReport($analytics) { // Replace with your view ID. E.g., XXXX. $VIEW_ID = "202020192018"; // Create the DateRange object. $dateRange = new Google_Service_AnalyticsReporting_DateRange(); $dateRange->setStartDate("7daysAgo"); $dateRange->setEndDate("today"); // Create the ReportRequest object. $request = new Google_Service_AnalyticsReporting_ReportRequest(); $request->setViewId($VIEW_ID); $request->setDateRanges($dateRange); // suggested by docs... but returns nothing? $userActivity = $analytics->userActivity; $body = new Google_Service_AnalyticsReporting_SearchUserActivityRequest(); $body->setViewId($VIEW_ID); $user = new Google_Service_AnalyticsReporting_User(); $user->setType('CLIENT_ID'); $user->setUserId('1000010001000.234234234'); $body->setUser($user); $output = $userActivity->search( $body ); echo " <pre>\n"; print "Rows: " . $output->getTotalRows() . "\n"; $sessions = $output->getSessions(); print "Sessions: " . count($sessions) . "\n"; for ( $rowIndex = 0; $rowIndex < count($sessions); $rowIndex++) { $row = $sessions[ $rowIndex ]; print "Session " . $rowIndex . ": " . $row->getDeviceCategory() . "\n"; echo "-- Activity types: " . count($row->getActivities()) . "\n"; $activities = $row->getActivities(); for ( $activityIndex = 0; $activityIndex < count($activities); $activityIndex++) { $activity = $activities[ $activityIndex ]; echo "-- -- Keyword: " . $activity->getKeyword() . "\t Campaign: " . $activity->getCampaign() . " Medium: " . $activity->getMedium() . "\n"; } } echo "</pre> "; } function printResults($reports) { for ( $reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++ ) { $report = $reports[ $reportIndex ]; $header = $report->getColumnHeader(); $dimensionHeaders = $header->getDimensions(); $metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries(); $rows = $report->getData()->getRows(); for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) { $row = $rows[ $rowIndex ]; $dimensions = $row->getDimensions(); $metrics = $row->getMetrics(); for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) { print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n"); } for ($j = 0; $j < count( $metricHeaders ) && $j < count( $metrics ); $j++) { $entry = $metricHeaders[$j]; $values = $metrics[$j]; print("Metric type: " . $entry->getType() . " \n" ); for ( $valueIndex = 0; $valueIndex < count( $values->getValues() ); $valueIndex++ ) { $value = $values->getValues()[ $valueIndex ]; print($entry->getName() . ": " . $value . "\n"); } } } } }