Filter for Google Analytics API - r

I'm trying to run this query with rGoogleAnalytics but it's throwing the error
Error in ParseDataFeedJSON(GA.Data) :
code : 400 Reason : Invalid value 'ga:pagePath=~/companies/[0-9]{6,8};ga:pagePath!#reviews' for filters parameter
I'm trying to fetch pages matching the pattern /companies/ followed by 6-8 numbers and not containing reviews
query.list <- Init(start.date = "2016-01-01",
end.date = "2017-03-31",
dimensions = "ga:pagePath",
metrics = "ga:pageviews",
filters = "ga:pagePath=~\/companies\/[0-9]{6,9};ga:pagePath!#reviews",
max.results = 10000,
table.id = "ga:xxxxxx")
Thanks

It appears that the problem is with your use of {6,9}. Perhaps you can Try to url encode that part of your regular expression: %7B6%2C9%7D
Use the Query Explorer to play with your query until you find one that works with what you are trying to accomplish.
The documentation states URL-reserved characters — Characters such as & must be url-encoded

Related

R httr GET doesn't like time format

I want to download data from a website using an api. It looks like the following but with the XXXs, YYYs and ZZZs replaced with api codes.
https://api.ecowitt.net/api/v3/device/history?application_key=XXXX&api_key=-YYY&mac=ZZZ&start_date=2022-06-01 00:00:00&end_date=2022-09-27 00:00:00&cycle_type=30min&call_back=outdoor,indoor.humidity
When I put this url into a web browser I get a page full of data so the server seems to be happy with it.
When I name it "url_complete" and run
response <- httr::GET(url_complete) I get this error message
Error in curl::curl_fetch_memory(url, handle = handle) : URL using
bad/illegal format or missing URL
With the start and end dates and times removed from the above url, The response Status is 200.
response <- httr::GET(test_url)
content(response, "text")
[1] "{"code":40000,"msg":"start_date require","time":"1664309668","data":[]}"
The solution was to pull the date strings out of the url and add them with a query as follows
response <- httr::GET(url_complete_noDates, add_headers(accept = 'application/json'), query = list(start_date = "2022-06-01 00:00:00", end_date = "2022-09-27 00:00:00"))
What this does is add some code to replace the special characters in the date/time string - space and colon.
start_date=2022-06-01%2000%3A00%3A00&end_date=2022-09-27%2000%3A00%3A00

GoogleAnalyticsR api - FilterExpression

I need to retrieve data from google analytics using R
I write the following code with GoogleAnalyticsR:
df <- google_analytics(viewId = my_id,
date_range=c(start,end),
metrics = c("pageViews"),
dimensions = "pagePath",
anti_sample = TRUE,
filtersExpression ="ga:pagePath==RisultatoRicerca?nomeCasa",
max=100000)
I need to set correctly the FiltersExpression parameters.
I 'd like to have data from pagePath that contains RisultatoRicerca?nomeCasa. This code returns me a dataframe with 0 rows, which i know it's impossible ( data from an e-commerce with more than ten thousand interaction per day). So i 've begun to think that my FiltersExpression is incorrect.
Thanks in advance
I managed to solve the problem using filtersExpression
filtersExpression = "ga:pagePath=#RisultatoRicerca?nomeCasa
this filter works on pagePath dimension and filter every path that contain RisultatoRicerca

since and until arguments cause an error in R

donald_tweets <- searchTwitter("Donald + Trump Republicans exclude:retweets",
n=50, lang = "en", since = "2016-03-16", until = "2016-03-17")
donald_tweets
But this gives me error.
Warning message:
In doRppAPICall("search/tweets", n, params = params, retryOnRateLimit = retryOnRateLimit
50 tweets were requested but the API can only return 0
and somewhere I have seen that this is the problem with since and until that these since and until search for fewer days. As it is 2018 not 2016. But what can I do in this regard? Please help! This is the project in R.
The Twitter Search Documentation contains two useful pieces of information.
It says that until:
Keep in mind that the search index has a 7-day limit. In other words, no tweets will be found for a date older than one week.
It also shows that there is no since parameter. It is since_id:
Returns results with an ID greater than (that is, more recent than) the specified ID. There are limits to the number of Tweets which can be accessed through the API. If the limit of Tweets has occured since the since_id, the since_id will be forced to the oldest ID available.
So there are the two errors in your code. You cannot search for anything older than a week. If you want to use a "since" parameter, you have to give it an ID, not a date.

RGoogleAnalytics Filtering Error

Hey guys so I have tried many different ways to do this query
query.list <- Init(start.date = "2016-09-19",
end.date = "2016-09-23",
dimensions = "ga:date,ga:hour,ga:minute,ga:country",
metrics = "ga:newUsers",
filters = "ga:source!=Emai, ga:country==United Kingdom",
max.results = 10000,
sort = "ga:date",
table.id = "ga:XXXX"
)
ga.query <- QueryBuilder(query.list)
ga.data2 <- data.table(GetReportData(ga.query, token, split_daywise = T )
)
I do not know why it doesn't filter the country. I have tried only filtering the country and it simply does not work, I'm sure is something real simple that I'm missing. But I have tried every recommendation in other questions and is still not working. If I take out the country filter it works and if I put it does not do anything.. just outputs the same data
Since you need to include AND operator, which is denoted by a semicolon ;. Moreover, you need to URL encode all the parameters in the filter, so the correct filter would be:
ga:source!=Email;ga:country%3D%3DUnited%20Kingdom
To all readers:
While building the query in R for core reporting, please make sure that all the values are URL encoded. If you feel any difficulty in encoding, you can build the query from Google's Query Explorer

How to filter dimension values when fetching data from Google Analytics into PowerQuery/PowerBi

I want to fetch data from Google Analytics With M (PowerQuery/PowerBI), but want to filter the dimension values on the fily, Let's Say my dimension is "Page" and I want "Pageviews" and "Unique Pageviews" measures, provided that the "Page" is following a regex filter (ex. ga:pagePath=~^.*?([0-9]{6,7}|mpg[0-9]{1,3}){1}\.html[/]?[^ ]*)
I could use "Table.SelectRows", but as M doesn't support regex, this filter should be passed to GA api directly. Here is what M generated for me:
let
Source = GoogleAnalytics.Accounts(),
#"1234567" = Source{[Id="1234567"]}[Data],
#"UA-987654-1" = #"1234567"{[Id="UA-52004541-1"]}[Data],
#"11111" = #"UA-987654-1"{[Id="1234567"]}[Data],
#"Added Items" = Cube.Transform(#"11111", {{Cube.AddAndExpandDimensionColumn, "ga:pagePath", {"ga:pagePath"}, {"Page"}}, {Cube.AddMeasureColumn, "Pageviews", "ga:pageviews"}, {Cube.AddMeasureColumn, "Unique Pageviews", "ga:uniquePageviews"}}),
#"Filtered Rows" = Table.SelectRows(#"Added Items", each Text.Contains([Page], "html"))
in
#"Filtered Rows"
Is there any possiblity to pass my regex filter to GA api in M?
Thanks for your interest!
We don't support regular expression filters today. With Text filters you can generally implement the same filter logic, but admittedly it wouldn't be as concise as your regex.
We welcome requests for new features at https://ideas.powerbi.com, so consider starting a thread there :)

Resources