0
votes

How can I access the full referral path for one session/user through Google Reporting API V4 ? In this case in PHP.

For example we have following code found on Google's Reporting API V4 Documentation. (https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-php)

function getReport(&$analytics) {

  // Replace with your view ID, for example XXXX.
  $VIEW_ID = "<REPLACE_WITH_VIEW_ID>";

  // Create the DateRange object.
  $dateRange = new Google_Service_AnalyticsReporting_DateRange();
  $dateRange->setStartDate("7daysAgo");
  $dateRange->setEndDate("today");

  // Create the Metrics object.
  $sessions = new Google_Service_AnalyticsReporting_Metric();
  $sessions->setExpression("ga:sessions");
  $sessions->setAlias("sessions");

  // Create the ReportRequest object.
  $request = new Google_Service_AnalyticsReporting_ReportRequest();
  $request->setViewId($VIEW_ID);
  $request->setDateRanges($dateRange);
  $request->setMetrics(array($sessions));

  $body = new Google_Service_AnalyticsReporting_GetReportsRequest();
  $body->setReportRequests( array( $request) );
  return $analytics->reports->batchGet( $body );
}

This part is interesting:

$sessions->setExpression("ga:sessions");
$sessions->setAlias("sessions");

Dimensions & Metrics Explorer (https://developers.google.com/analytics/devguides/reporting/core/dimsmets)

The path of the referring URL (e.g., document.referrer). If someone places on their webpage a link to the property, this is the path of the page containing the referring link.

The full referring URL including the hostname and path.

I am assuming that I have to go this way just fetching the desired dimensions/metrics:

$sessions->setExpression("ga:referralPath");
$sessions->setAlias("referral_path");

or

$sessions->setExpression("ga:fullReferrer");
$sessions->setAlias("full_referrer");

Would be this the right approach? If not is there another way to accomplish this?

And another question: When making a request with this metrics/dimensions:

$sessions->setExpression("ga:referralPath");
$sessions->setAlias("referral_path");

How Google knows from which session to take the referralPath?

1

1 Answers

0
votes

Try to read through Traffic Sources - Dimensions and Metrics, this reference document lists and describes all the dimensions and metrics available through the Real Time Reporting API.

Here's a sample of dimension: rt:referralPath - The path of the referring URL (e.g. document.referrer). If someone places a link to your property on their website, this element contains the path of the page that contains the referring link. This value is only set when rt:medium=referral.

Note: Use Google Analytics superProxy to handle many of the implementation details of working with Google Analytics APIs on authentication, caching, and transforming API responses to formats used directly with visualization and chart libraries.

You may also try to read Management API, this API is a guides that will help you initially get an application up and running and then the documentation will dive into the various topics which should help you interact with the API to perform such things as account, user, and data management. There is also a complete set of reference documents, which give details of every parameter of each API endpoint and include API sample code.