I have to make a PHP script, which will automatically upload data to Google Analytics using API. But there is a problem: developer guide is written for python and java only. Google APIs client library for PHP already have upload method. Please, help me to translate into PHP this java code:
File file = new File("data.csv");
InputStreamContent mediaContent = new InputStreamContent('application/octet-stream', new FileInputStream(file));
mediaContent.setLength(file.length());
Upload upload = analytics.management().dailyUploads().upload("1234",
"UA-1234-1", "123456789", "2012-10-31", 1, "cost", mediaContent);
upload.setReset(true);
DailyUploadAppend append = upload.execute();
or this python code:
media = MediaFileUpload('data.csv', mimetype='application/octet-stream', resumable=False)
daily_upload = analytics.management().dailyUploads().upload(
accountId='1234',
webPropertyId='UA-1234-1',
customDataSourceId='123456789',
date='2012-10-31',
appendNumber=1,
reset=true,
type='cost',
media_body=media).execute()
Have you gotten this to work yet? What have you tried? I’m no expert but I’ll take a crack at it.
The PHP library on their site definitely supports this, there are methods for “dailyUploads” listed in the Google APIs Client Library for PHP.
This all came from the class “Google_ManagementDailyUploadsServiceResource”.
The connection will need to be authenticated. If your input file contains data for multiple days you can include an end date.