I am using a fairly simple piece of code to send events to a Google Analytics account:
$req = curl_init('https://www.google-analytics.com/collect');
curl_setopt_array($req, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POSTFIELDS =>
"v=1&t=event&tid=UA-40825301-52&cid=123456&ec=test&ea=test2&el=test3&ev=123&utmcsr=google&utmcmd=organic"
));
$response = curl_exec($req);
What I am trying to achieve is sending offline conversions to our Google Analytics as events. We do know the initial source of these conversions and want this data in Google Analytics too. utmcsr and utmcmd are supposed to be used to send source & medium data but.. all events end up as direct traffic. Any idea what might be the issue?
What you're using is called Measurement protocol. There are multiple comfortable libraries to use it. You still can use it through curl, but then I don't recognize the utmcsr and utmcmd. Where are they from?
Here is the parameter reference for it: https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters I don't see there the parameters you're trying to pass in your request.
The utm params are a part of the dl parameter. The document location. You can inspect an existing collect call and see how this info is passed. Here, I used SO's existing tracking, just faked the utm params:
Disregard all cd parameters and the dp. You don't seem to be needing them for now.
Feel free to explore the measurement protocol properly starting from here: https://developers.google.com/analytics/devguides/collection/protocol/v1
It appears that using the cm and cs parameters allow you to post source and medium to Google Analytics with server side analytics tracking.
What helped me tremendously:
https://ga-dev-tools.web.app/hit-builder/
https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters
My code:
$req = curl_init('https://www.google-analytics.com/collect');
curl_setopt_array($req, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POSTFIELDS =>
"v=1&t=transaction&tid=UA-40825301-52&cid=e87f9f6f-fba1-4922-9319-98e3c1d6f7c6&dp=%2Freceipt&dt=Receipt%20Page&ti=T12345&ta=Direct&tr=37.39&tt=2.85&ts=5.34&tcc=SUMMER2013&pa=purchase&pr1id=P12345&pr1nm=Android%20Warhol%20T-Shirt&pr1ca=Apparel&pr1br=Google&pr1va=Black&pr1ps=1&utm_source=google&utm_medium=ads&ds=web&cs=google&cm=organic"
));
$response = curl_exec($req);
$curl = curl_init();
It's possible to set age restriction individually/manually via the Web UI, but it's not possible to set globally for a channel or account as far as I can see.
Is it possible to set via API when uploading a video?
The point is to avoid banning if I upload a video on behalf of another user, and I don't get time to screen the video.
Thanks in advance,
Anders
Update 1:
This is how I found I should do, according to https://developers.google.com/youtube/v3/docs/videos:
$contentRating = new Google_Service_YouTube_ContentRating();
$contentRating->setYtRating('ytAgeRestricted');
$details = new Google_Service_YouTube_VideoContentDetails();
$details->setContentRating($contentRating);
$video->setContentDetails($details);
Remarkably there's not a single example of use of Google_Service_YouTube_VideoContentDetails anywhere.
When this data is added to the video, the upload fails with the following. Clearly YouTube didn't like my setting. Is the use of age-restriction in itself restricted?
Google_Exception::__set_state(array(
'message' => 'Failed to start the resumable upload (HTTP 400: youtube.part, contentDetails)',
'string' => '',
'code' => 0,
'file' => '/home/u/u6554025/www/twitizer.com/Google/Http/MediaFileUpload.php',
'line' => 300,
Update 2:
That's a bummer if true:
http://gadgetsytecnologia.com/de585c5edb2f3a366/uploading-video-via-youtube-api.html
"You can read this property as: contentDetails.contentRating.ytRating = "ytAgeRestricted" but it looks like you can't POST this property in the video Resource in the body of your POST request to Youtube API"
Neither does the official documentation mention that insert supports contentRating.
Live and learn. Adopt, adapt, improve.
If anyone has any idea how to circumvent this, possibly with a global setting for the channel, I'm all ears.
Thanks,
Anders
I've developed a WordPress plugin to pull videos from YouTube with the YouTube v3 API
The plugin works fine in most sites but currently I'm having an issue with one, I can't seem to pass the error:
"Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
The weird thing is I can copy / paste the query with a valid API key on the browser and it works, I've tested with other Api Keys with the same results. It seems the problem is with that server
I've checked than other plugins were using the Google API, but disabled all of them and still getting the same error
I don't know what to do next
Here is an example of a basic query to the YouTube API
$request = new WP_Http;
$result = $request->request(
'https://www.googleapis.com/youtube/v3/channels?part=id&forUsername=USERNAME&key=API_KEY',
array(
'method' => 'GET',
'headers' => array('Accept' => 'application/json' )
)
);
The request is handled by the native WordPress class WP_Http
I'm trying to develop a Play Store reviews scraper in PHP and I need to make a POST request to this URL https://play.google.com/store/getreviews, and I saw the parameter post with firebug.
I am using Goutte library and here is my code:
require_once 'goutte.phar';
use Goutte\Client;
$client = new Client();
$params = Array(
"id" => "com.trello",
"pageNum" => 2 ,
"reviewSortOrder" => 2 ,
"reviewType" => 0,
"xhr" => 1
);
$crawler = $client->request('POST' , 'https://play.google.com/store/getreviews', $params);
The problem is that the request returns nothing. Is there anyone who already faced this problem and solved it?
I don't think this is possible. Google Play changed their review interface last year. They now have a "token" parameter which is missing here. I have worked before to try and work out what seeds this (see Google play review scraping changes) but I can't figure it out. After a number of attempts to hit that webservice with an incorrect request (presumably without the token) Google Play starts blocking your IP, that's why you'll be getting nothing back after a while (and won't be able to open Google Play in your browser either). If you find a solution, let me know!
This URL works for me, with the form-post data in your example.
https://play.google.com/store/getreviews?authuser=0
I'm trying to use the Google API v3 to access one google calendar and according to the documentation here : http://code.google.com/apis/calendar/v3/using.html#intro and here : https://code.google.com/apis/console/, the solution I need is the "Simple API Access" & "Key for server apps (with IP locking)".
Now, when I create a page with this code :
session_start();
require_once 'fnc/google-api-php-client/src/apiClient.php';
require_once 'fnc/google-api-php-client/src/contrib/apiCalendarService.php';
$apiClient = new apiClient();
$apiClient->setUseObjects(true);
$service = new apiCalendarService($apiClient);
if (isset($_SESSION['oauth_access_token'])) {$apiClient->setAccessToken($_SESSION['oauth_access_token']);
} else {
$token = $apiClient->authenticate();
$_SESSION['oauth_access_token'] = $token;
}
and in my "config.php" file I add ONLY my developper key (in place of the "X") :
global $apiConfig;
$apiConfig = array(
// True if objects should be returned by the service classes.
// False if associative arrays should be returned (default behavior).
'use_objects' => false,
// The application_name is included in the User-Agent HTTP header.
'application_name' => '',
// OAuth2 Settings, you can get these keys at https://code.google.com/apis/console
'oauth2_client_id' => '',
'oauth2_client_secret' => '',
'oauth2_redirect_uri' => '',
// The developer key, you get this at https://code.google.com/apis/console
'developer_key' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
// OAuth1 Settings.
// If you're using the apiOAuth auth class, it will use these values for the oauth consumer key and secret.
// See http://code.google.com/apis/accounts/docs/RegistrationForWebAppsAuto.html for info on how to obtain those
'oauth_consumer_key' => 'anonymous',
'oauth_consumer_secret' => 'anonymous',
But then I get errors and it tells me it's trying to authenticate using the "OAuth 2.0" system which I don't want to use. I only want to access one calendar with an API key.
And amazingly, when I search in google "Simple API Access key" I find nothing, nothing on their docs, no examples, no tutorials, nothing. Am I the only one using this thing?
So can someone tell me what I'm doing wrong?
(i know this is an old question but i would've been glad if someone
gave a real answer here so i'm doing it now)
I came on the same problem, Simple API access is not well documented (or maybe just not where i searched), but using the Google API Explorer i found a way to get what i need, which is in fact pretty straightforward. You don't need specific lib or anything : it's actually really simple.
In my case i simply needed to search a keyword on G+, so i just had to do a GET request:
https://www.googleapis.com/plus/v1/activities?query={KEYWORD}&key={YOUR_API_KEY}
Now, for a calendar access (see here), let's pretend we want to fetch access control rules list. We need to refer to calendar.acl.list which give us the URI :
https://www.googleapis.com/calendar/v3/calendars/{CALENDAR_ID}/acl?key={YOUR_API_KEY}
Fill in the blanks, and that's pretty much all you need to do. Get a server key (API Access submenu), store it somewhere in your project and call it within URIs you're requesting.
You cannot access your calendar information using API Key. API keys (or simple API acess key) are not authorized tokens and can only be used for some API calls such as a Google search query etc; API keys will not let you access any user specific data, which I am assuming is your objective through this calendar application.
Also, from what I see in your code, you are creating a client object which is going to use OAuth 2.0 authentication and hence you are getting authentication error messages.
There is no such a thing called Simple API Access key.
Normally OAuth 2.0 is used for authorization. But since you have your reason not to use it.
If you want to use OAuth1.0 for authorization. You need an API key in Simple API Access section on the API Access page.
If you want to use username & password login instead of OAuth, you can refer to ClientLogin, but this is not recommanded.
I got to this thread when trying to do the same today. Although this is way late, but the answer is YES, there is actually simple API key for those apis that does not need user authorizations, and the official client library support this.
The api library do this by Options, which is key, value pair.
Take the example of get information of a given youtube video, you would use this api: https://godoc.org/google.golang.org/api/youtube/v3#VideosListCall.Do
To use api key, simply make a type that implements the CallOption interface, and let it return the api key:
type APIKey struct {
}
func (k *APIKey) Get() (string, string) {
return "key", "YOU API KEY HERE"
}
Then when calling the API, supply the APIKey to it:
youtube, err := youtube.New(&http.Client{})
call := youtube.Videos.List("snippet,contentDetails,statistics").Id(id)
rsp, err := call.Do(opt)
This way, you can construct the youtube client with the vallina http client, rather than oauth client, and enjoy the simple api key.
The first answer said you can use http GET directly, but then you will need to handle the errors and parse the result yourself.
See below link which is helpfull to you. The Google API Client Library enables you to work with Google APIs such as Analytics, Adsense, Google+, Calendar, Moderator, Tasks, or Latitude on your server, in the language of your choice.
http://code.google.com/p/google-api-php-client/
Thanks,
Chintu