Uploads: uploadData

अनुमति की ज़रूरत है

कस्टम डेटा सोर्स के लिए डेटा अपलोड करें. उदाहरण देखें.

यह तरीका /upload यूआरआई के साथ काम करता है और अपलोड किए गए मीडिया को इन विशेषताओं के साथ स्वीकार करता है:

  • फ़ाइल का ज़्यादा से ज़्यादा साइज़: 1 जीबी
  • स्वीकार किए गए मीडिया MIME टाइप: application/octet-stream

स्टैंडर्ड पैरामीटर के अलावा, यह तरीका पैरामीटर टेबल में दिए गए पैरामीटर के साथ काम करता है.

अनुरोध

एचटीटीपी अनुरोध

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 /upload यूआरआई के लिए अपलोड करने के अनुरोध का टाइप. स्वीकार की जाने वाली वैल्यू ये हैं:
  • media - आसान अपलोड. मीडिया डेटा अपलोड करें.
  • multipart - कई हिस्सों में अपलोड. मेटाडेटा के साथ फ़ाइल अपलोड करें (जैसे कि फ़ाइल का नाम). अनुरोध के मुख्य हिस्से के दो हिस्से होने चाहिए: पहला, मेटाडेटा के लिए और दूसरा, फ़ाइल के डेटा के लिए.
  • resumable - फिर से शुरू किया जा सकने वाला अपलोड. कम से कम दो अनुरोधों की सीरीज़ का इस्तेमाल करके, फ़ाइल को फिर से शुरू करने लायक तरीके से अपलोड करें.

अनुमति

इस अनुरोध के लिए, इनमें से कम से कम एक स्कोप के साथ अनुमति देना ज़रूरी है (पुष्टि करने और अनुमति देने के बारे में ज़्यादा पढ़ें).

स्कोप
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. });
  }
}