Uploads: uploadData

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

อัปโหลดข้อมูลสำหรับแหล่งข้อมูลที่กำหนดเอง ดูตัวอย่าง

เมธอดนี้รองรับ URI /upload และยอมรับสื่อที่อัปโหลดซึ่งมีลักษณะเฉพาะต่อไปนี้

  • ขนาดไฟล์สูงสุด: 1 GB
  • ประเภท MIME ของสื่อที่ยอมรับ application/octet-stream

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

ส่งคำขอ

คำขอ HTTP

POST https://www.googleapis.com/upload/analytics/v3/management/accounts/accountId/webproperties/webPropertyId/customDataSources/customDataSourceId/uploads

พารามิเตอร์

ชื่อพารามิเตอร์ ค่า คำอธิบาย
พารามิเตอร์เส้นทาง
accountId string รหัสบัญชีที่เชื่อมโยงกับการอัปโหลด
customDataSourceId string รหัสแหล่งข้อมูลที่กำหนดเองซึ่งเป็นที่อยู่ของข้อมูลที่อัปโหลด
webPropertyId string สตริง UA ของพร็อพเพอร์ตี้เว็บที่เชื่อมโยงกับการอัปโหลด
พารามิเตอร์การค้นหาที่จำเป็น
uploadType string ประเภทของคำขออัปโหลดไปยัง URI /upload ค่าที่ยอมรับมีดังนี้

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

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

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

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

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

คำตอบ

หากสำเร็จ เมธอดนี้จะแสดงทรัพยากรการอัปโหลดในเนื้อหาการตอบสนอง

ตัวอย่าง

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

Java

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

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


// This request uploads a file for the authorized user.
File file = new File("data.csv");
InputStreamContent mediaContent = new InputStreamContent("application/octet-stream",
    new FileInputStream(file));
mediaContent.setLength(file.length());
try {
  analytics.management().uploads().uploadData("123456",
      "UA-123456-1", "122333444455555", mediaContent).execute();
} catch (GoogleJsonResponseException e) {
  System.err.println("There was a service error: "
      + e.getDetails().getCode() + " : "
      + e.getDetails().getMessage());
}

PHP

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

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

/**
 * This request uploads a file to a custom data source.
 */
try {
  $analytics->management_uploads->uploadData(
      '123456',
      'UA-123456-1',
      '122333444455555',
      array('data' => file_get_contents('example.csv'),
            'mimeType' => 'application/octet-stream',
            'uploadType' => 'media'));

} 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();
}

Python

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

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


# This request uploads a file custom_data.csv to a particular customDataSource.
# Note that this example makes use of the MediaFileUpload Object from the
# apiclient.http module.
from apiclient.http import MediaFileUpload
try:
  media = MediaFileUpload('custom_data.csv',
                          mimetype='application/octet-stream',
                          resumable=False)
  daily_upload = analytics.management().uploads().uploadData(
      accountId='123456',
      webPropertyId='UA-123456-1',
      customDataSourceId='9876654321',
      media_body=media).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))

JavaScript

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

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

/*
 * This request uploads a file for the authorized user.
 */
function uploadData(fileData) {
  const boundary = '-------314159265358979323846';
  const delimiter = "\r\n--" + boundary + "\r\n";
  const close_delim = "\r\n--" + boundary + "--";

  var contentType = 'application/octet-stream'

  var reader = new FileReader();
  reader.readAsBinaryString(fileData);
  reader.onload = function(e) {
    var contentType = 'application/octet-stream';
    var metadata = {
      'title': fileData.name,
      'mimeType': contentType
    };

    var base64Data = btoa(reader.result);
    var multipartRequestBody =
        delimiter +
        'Content-Type: application/json\r\n\r\n' +
        JSON.stringify(metadata) +
        delimiter +
        'Content-Type: ' + contentType + '\r\n' +
        'Content-Transfer-Encoding: base64\r\n' +
        '\r\n' +
        base64Data +
        close_delim;

    var request = gapi.client.request({
      'path': 'upload/analytics/v3/management/accounts/123456/webproperties/UA-123456-1/customDataSources/ABCDEFG123abcDEF123/uploads',
      'method': 'POST',
      'params': {'uploadType': 'multipart'},
      'headers': {
        'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
      },
      'body': multipartRequestBody
    });
  request.execute(function (response) { // Handle the response. });
  }
}