0

UPDATE: Cross posting in Google Groups to draw more attention.

I manage to get my php client using Google Analytics v3 API.

However, I have trouble navigating to get the bounce rate based on 3 different segments:

My code is below:

// create service and get data
$service = new Google_Service_Analytics($client);

// ids - Unique table ID for retrieving Analytics data. Table ID is of the form ga:XXXX, where XXXX is the Analytics view (profile) ID.
// startDate - Start date for fetching Analytics data. Requests can specify a start date formatted as YYYY-MM-DD, or as a relative date (e.g., today, yesterday, or 7daysAgo). The default value is 7daysAgo.
// endDate - End date for fetching Analytics data. Request can should specify an end date formatted as YYYY-MM- DD, or as a relative date (e.g., today, yesterday, or 7daysAgo). The default value is yesterday.
// metrics - A comma-separated list of Analytics metrics. E.g., 'ga:sessions,ga:pageviews'. At least one metric must be specified.
$ids = 'ga:59542xxx';
$startDate = '2014-01-01';
$endDate = '2014-01-31';
$metrics = 'ga:bounces';
$optParams = array('segment' => 'users::sequence::ga:userType==New Vistor');
$call = $service->data_ga->get($ids, $startDate, $endDate, $metrics, $optParams);

My code is largely based on this link.

The 3 segments I want is Organic Traffic, New Users, Returning Users.

I know the condition is

ga:userType==New Visitor
ga:userType==Returning Visitor
ga:medium==organic

My questions are:

  1. do I need to send 3 separate calls?

  2. How do I structure the conditionScope and conditionType? Based on https://developers.google.com/analytics/devguides/reporting/core/v3/segments#reference

Community
  • 1
  • 1
Kim Stacks
  • 10,202
  • 35
  • 151
  • 282

1 Answers1

3
  1. Yes, as you can only apply a single segment at a time.
  2. You are likely looking to get this information at the session (visit) scope, rather than the user scope, as some items wouldn't make sense at the user scope (i.e. a user who visits twice would be both a new visitor and a returning visitor in the user scope, and bounce rate is a session-level metric); so your conditionScope will be sessions::. In terms of conditionType, these are simple conditions rather than sequences (see some info regarding sequence segments), so you'll be using condition::. Thus based on the <conditionScope><conditionType><dimensionOrMetricConditions> format, your segments would be:

    sessions::condition::ga:userType==New Visitor

    sessions::condition::ga:userType==Returning Visitor

    sessions::condition::ga:medium==organic

Additionally, you should be aware that ga:bounces will give you the total number of bounces in your request, rather than the bounce rate; you can use ga:bounceRate for bounce rate or additionally pull in ga:sessions to calculate the rate yourself.

Sean Adams
  • 453
  • 3
  • 7