Segments: list

ต้องมีการให้สิทธิ์

แสดงรายการกลุ่มที่ผู้ใช้มีสิทธิ์เข้าถึง ลองใช้เลยหรือดูตัวอย่าง

นอกเหนือจากพารามิเตอร์มาตรฐาน เมธอดนี้ยังรองรับพารามิเตอร์ที่แสดงในตารางพารามิเตอร์ด้วย

ส่งคำขอ

คำขอ HTTP

GET https://www.googleapis.com/analytics/v3/management/segments

พารามิเตอร์

ชื่อพารามิเตอร์ ค่า คำอธิบาย
พารามิเตอร์การค้นหาที่ไม่บังคับ
max-results integer จำนวนกลุ่มสูงสุดที่จะรวมในการตอบกลับนี้
start-index integer ดัชนีของกลุ่มแรกที่เรียกข้อมูล ใช้พารามิเตอร์นี้เป็นกลไกการใส่เลขหน้าร่วมกับพารามิเตอร์ max-results

การให้สิทธิ์

คำขอนี้ต้องได้รับการให้สิทธิ์อย่างน้อย 1 ขอบเขตต่อไปนี้ (อ่านเพิ่มเติมเกี่ยวกับการตรวจสอบสิทธิ์และการให้สิทธิ์)

ขอบเขต
https://www.googleapis.com/auth/analytics
https://www.googleapis.com/auth/analytics.edit
https://www.googleapis.com/auth/analytics.readonly

เนื้อหาของคำขอ

อย่าแสดงเนื้อหาของคำขอด้วยวิธีนี้

คำตอบ

คำตอบจะมีทรัพยากรกลุ่ม 1 กลุ่มสำหรับกลุ่มเป้าหมายขั้นสูงแต่ละกลุ่มที่ผู้ใช้ใช้งานได้

{
  "kind": "analytics#segments",
  "username": string,
  "totalResults": integer,
  "startIndex": integer,
  "itemsPerPage": integer,
  "previousLink": string,
  "nextLink": string,
  "items": [
    management.segments Resource
  ]
}
ชื่อพร็อพเพอร์ตี้ ค่า คำอธิบาย Notes
kind string ประเภทคอลเล็กชันของกลุ่ม
username string รหัสอีเมลของผู้ใช้ที่ตรวจสอบสิทธิ์แล้ว
totalResults integer จำนวนผลลัพธ์ทั้งหมดสำหรับข้อความค้นหา โดยไม่คำนึงถึงจำนวนผลลัพธ์ในการตอบกลับ
startIndex integer ดัชนีเริ่มต้นของทรัพยากร ซึ่งเท่ากับ 1 โดยค่าเริ่มต้น หรือระบุโดยพารามิเตอร์การค้นหา start-index
itemsPerPage integer จำนวนทรัพยากรสูงสุดที่การตอบกลับมีได้ โดยไม่คำนึงถึงจำนวนทรัพยากรที่แสดงผลจริง ค่าของช่วงคือ 1 ถึง 1000 โดยมีค่า 1000 โดยค่าเริ่มต้น หรือมีการระบุโดยพารามิเตอร์การค้นหา max-results
items[] list รายการกลุ่ม

ตัวอย่าง

หมายเหตุ: ตัวอย่างโค้ดที่มีสำหรับวิธีการนี้ไม่ได้แสดงถึงภาษาโปรแกรมที่รองรับทั้งหมด (ดูรายการภาษาที่รองรับได้ในหน้าไลบรารีของไคลเอ็นต์)

PHP

ใช้ไลบรารีของไคลเอ็นต์ PHP

/**
 * Note: This code assumes you have an authorized Analytics service object.
 * See the Segments Developer Guide for details.
 */

/**
 * Example #1:
 * Requests a list of all Segments for the authorized user.
 */
try {
  $segments = $analytics->management_segments->listManagementSegments();
} catch (apiServiceException $e) {
  print 'There was an Analytics API service error '
      . $e->getCode() . ':' . $e->getMessage();

} catch (apiException $e) {
  print 'There was a general API error '
      . $e->getCode() . ':' . $e->getMessage();
}


/*
 * Example #2:
 * The results of the list method are stored in the segments object.
 * The following code shows how to iterate through them.
 */
foreach ($segments->getItems() as $segment) {
  $html .= <<<HTML
<pre>

Segment ID = {$segment->getId()}
Kind       = {$segment->getKind()}
Self Link  = {$segment->getSelfLink()}
Name       = {$segment->getName()}
Definition = {$segment->getDefinition()}
Created    = {$segment->getCreated()}
Updated    = {$segment->getUpdated()}

</pre>
HTML;
  print $html;
}



Python

ใช้ไลบรารีของไคลเอ็นต์ Python

# Note: This code assumes you have an authorized Analytics service object.
# See the Segments Developer Guide for details.

# Example #1:
# Requests a list of segments to which the user has access.
try:
  segments = analytics.management().segments().list().execute()

except TypeError, error:
  # Handle errors in constructing a query.
  print 'There was an error in constructing your query : %s' % error

except HttpError, error:
  # Handle API errors.
  print ('There was an API error : %s : %s' %
         (error.resp.status, error.resp.reason))

# Example #2:
# The results of the list method are stored in the segments object.
# The following code shows how to iterate through them.
for segment in segments.get('items', []):
  print 'Segment Id         = %s' % segment.get('id')
  print 'Segment kind       = %s' % segment.get('kind')
  print 'Segment segmentId  = %s' % segment.get('segmentId')
  print 'Segment Name       = %s' % segment.get('name')
  print 'Segment Definition = %s' % segment.get('definition')
  if segment.get('created'):
    print 'Created    = %s' % segment.get('created')
    print 'Updated    = %s' % segment.get('updated')
  print

JavaScript

ใช้ไลบรารีของไคลเอ็นต์ JavaScript

/*
 * Note: This code assumes you have an authorized Analytics client object.
 * See the Segment Developer Guide for details.
 */

/*
 * Example 1:
 * Requests a list of all Segments for the authorized user.
 */
function listSegments() {
  var request = gapi.client.analytics.management.segments.list();
  request.execute(printSegments);
}

/*
 * Example 2:
 * The results of the list method are passed as the results object.
 * The following code shows how to iterate through them.
 */
function printSegments(results) {
  if (results && !results.error) {
    var segments = results.items;
    for (var i = 0, segment; segment = segments[i]; i++) {
      console.log('Segment Id: ' + segment.id);
      console.log('Segment Kind: ' + segment.kind);
      console.log('Segment Name: ' + segment.name);
      console.log('Segment Definition: ' + segment.definition);

      // These fields are only set for custom segments and not default segments.
      if (segment.created) {
        console.log('Created: ' + segment.created);
        console.log('Updated: ' + segment.updated);
      }
    }
  }
}

ลองใช้เลย

ใช้ API Explorer ด้านล่างเพื่อเรียกใช้เมธอดนี้ในข้อมูลสดและดูการตอบสนอง หรือลองใช้เครื่องมือสำรวจแบบสแตนด์อโลน