I need to upload a CSV to Google Analytics using the API. Everything goes OK, but file name in column shows "Unknown filename".
$analytics = new \Google_Service_Analytics($client);
try {
$analytics->management_uploads->uploadData(
'<acc id>',
'UA-xxx',
'xxxxxxxxxxxxxx',
array(
'data' => file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/ex.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();
}
Is it possible to post file with flename? Thank you!
It looks like its an issue more with the client library then the Google analytics API.
consequence of the client libraries stripping the filename from POST body information
Management API Cost Data Upload .csv Doesn't Have Filename
You can get around that by not using the client library.
import pycurl
import json
def curlUpload(accountId, webPropertyId, customDataSourceId, csv):
credential_path = 'PATHTOYOURCREDENTIALSJSONFILE'
f = open(credential_path)
googleAccessToken = json.loads(f.read())['access_token']
c = pycurl.Curl()
c.setopt(c.URL, 'https://www.googleapis.com/upload/analytics/v3/management/accounts/' + accountId + '/webproperties/' + webPropertyId + '/customDataSources/' + customDataSourceId + '/uploads?access_token=' + googleAccessToken)
c.setopt(c.HTTPPOST, [
('fileupload', (
c.FORM_FILE, csv,
c.FORM_CONTENTTYPE, 'application/octet-stream'
)),
])
c.perform()
c.close()