Reputation: 1326
I'd like to download data pertaining to keywords straight into/from R. I understand that RCurl would most probably be the way to go, but I am not sure how to proceed with the task. Perhaps anybody here could put me on the right track?
PS. I may slightly edit this question as answers pour in because I have some ideas of how I could possibly download Adwords data using R; however, the ideas are unclear, and any answers would probably make them clearer.
Many thanks.
EDIT: My attempt
The following is what I tried until now.
1. apply getURL
on the login URL to identify the ids
of the Email and Password fields
require(RCurl)
loginURL<- "https://accounts.google.com/ServiceLogin?service=adwords"
ch<- getCurlHandle()
curlSetOpt(curl=ch,ssl.verifypeer=FALSE,cainfo=system.file("CurlSSL", "cacert.pem", package = "RCurl"),cookiejar="./cookies.txt",cookiefile="./cookies.txt",verbose=TRUE,header=TRUE,followlocation=TRUE,autoreferer=TRUE)
try1<- getURL(loginURL,curl=ch)
2. I identified what the ids
for the important fields (email & password) were
<div class="email-div">
<label for="Email"><strong class="email-label">Email</strong></label>
<input type="email" spellcheck="false"
name="Email" id="Email" value=""
>
</div>
<div class="passwd-div">
<label for="Passwd"><strong class="passwd-label">Password</strong></label>
<input type="password" name="Passwd" id="Passwd"
3. I then used the above fields to apply the postForm
function at the loginURL
in order to log into Google Adwords
params<- list(
"Email"="myemail",
"Passwd"="mypassword",
"GALX"="3b6rR7Jvk30")
loggedIn<- postForm(loginURL,.params=params,curl=ch)
However, I have no idea how to verify that I have successfully logged in.
Plus, the URL for Kyeword planner tool in Google Adwords UI is:
https://adwords.google.com/ko/KeywordPlanner/Home?__c=XXXXXXXXXX&__u=XXXXXXXXXX&__o=cues
where the c= reflects the customer id
and the u= reflects the user id
. What I thought about doing, given this, was to log in using my browser, paste the URL shown above into R, and then try to find out the ids for fields that I will be of relevance to me, such as the id for the keywords text box
in the Keyword planner tool, to which I could possibly send keywords from R.
But when I try to apply getURL
on the aforesaid website, I do not get the required/expected xml tags
or key value pairs
. Instead:
<html><head><noscript><meta http-equiv="refresh" content="0; URL=https://adwords.google.com/select/interstitial_short_js.html"></noscript></head><body><script type="text/javascript" language="javascript">var jsRedirect = true;var url = "/um/StartNewLogin?dst=/ko/KeywordPlanner/Home?__c%3D7857647860%26__u%3D4575929980%26__o%3Dcues";
if (self.document.location.hash) {url = url + ((url.indexOf('?') == -1)? '?' : '&') + "frag=" + self.document.location.hash.substring(1); }
window.location.assign(url);
</script> </body> </html>
This leads me to think that I am probably dealing with Javascript
or AJAX
here.
So, how to extract data from Javascript
or AJAX
using RCurl
, and is this the correct question to ask?
Thanks & apologies for the lengthy edit.
Upvotes: 3
Views: 1991
Reputation: 675
Have you seen our RAdwords package in the meantime?
It provides an authentication process for R with the Adwords API and an interface to load data from the Adwords API directly into R.
Example code to load keyword data:
#install package from CRAN
install.packages('RAdwords')
#load package
library(RAdwords)
#start authentication process
google_auth <- doAuth()
#build statement object
body <- statement(select=c('AccountDescriptiveName','Date', 'CampaignName', 'AdGroupName','KeywordText', 'KeywordMatchType', 'Clicks', 'Cost'),
report="KEYWORDS_PERFORMANCE_REPORT",
start="20140320",
end="20140321")
#download data as data frame
data <- getData(clientCustomerId='xxx-xxx-xxxx',
google_auth = google_auth,
statement=body,
transformation = T)
#all available report types
reports()
#all available metrics of specific report type
metrics("KEYWORDS_PERFORMANCE_REPORT")
Upvotes: 3
Reputation: 2482
I don't have enough reputation to comment (ridiculous!), hence this answer.
My answer pertains to your question's part on how to verify the result of logging in. I would do it this way:
loggedIn<- postForm(loginURL, .params=params, curl=ch)
info <- getCurlInfo(ch)
# 1) check the value of info$response.code here and act accordingly
# 2) you probably need the value to be 200 or between 200 and 300, but for more details
# on HTTP status codes, see this page: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
Upvotes: 2