Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using split_daywise = T results in dates being 1 day behind submitted dates #28

Open
BobbyBarbeau opened this issue Jun 7, 2016 · 1 comment

Comments

@BobbyBarbeau
Copy link

I'm having an issue where start and end dates are off by 1 day.

The following is a toy example, but I am able to reproduce the error on my machine using this. (The query I want to run is for the full month, which is why I'm using split_daywise = T, delay = 5 )

load("token_file") # in the actual code I use a full local path to the token file

ValidateToken(token)

query.list <- Init(start.date = "2016-05-01",
                   end.date = "2016-05-02",
                   dimensions = "ga:date,ga:source,ga:medium,ga:deviceCategory",
                   metrics = "ga:sessions",
                   max.results = 10000,
                   table.id = "ga:xxxxxxxxxx"
)

ga.query <- QueryBuilder(query.list)

ga.data <- GetReportData(ga.query, token, split_daywise = T, delay = 5)

This will return:

Access Token is valid.
[ Run 0 of 1] Getting data for 2016-04-30
[ Run 1 of 1] Getting data for 2016-05-01
The API returned 460 results.

Anyone else experiencing this or have an idea what could be causing it?

The only date arithmetic I've found in the execution of the above is in SplitQueryDaywise() in line 43 where date <- format(as.POSIXct(start.date) + days(i), '%Y-%m-%d') but it's addition, and my issue is that 1 day is being subtracted.

If I execute that code directly in the console, the calculation is performed correctly, returning "2016-05-01" for i = 0 and start.date = "2016-05-01"

When I set a breakpoint at line 41 in SplitQueryDaywise() and step through the calculation, everything looks fine. I hit next and i is set to 0. I hit next again, and date <- format(as.POSIXct(start.date) + days(i), '%Y-%m-%d') evaluates to "2016-04-30"

Also, if I drop the split_daywise and delay arguments, the ga:date data shows that the data is correctly from 5/1 and 5/2. Unfortunately if I try to get the whole month, it introduces sampling, which I want to avoid. But if I split daywise, the dates are off.

System info in case it's relevant:
Windows 7 32 bit
R version 3.3.0
Rstudio 0.99.893
RGoogleAnalytics 0.1.5
lubridate 1.5.6
httr 1.1.0
digest 0.6.9

@BobbyBarbeau
Copy link
Author

I've been able to work around it by replacing as.POSIXct(start.date) in line 43 of SplitQueryDaywise.R with either as.Date(start.date) or as.POSIXlt(start.date). With both alternatives, the date artithmetic works for me then.

However, I still don't understand why the error only appeared during execution of SplitQueryDaywise() but executing the same code directly in the console produced no error:

start.date <- "2016-05-01"
i <- 0
date <- format(as.POSIXct(start.date) + days(i), '%Y-%m-%d')
date
[1] "2016-05-01"

I also don't know what the implications are of using as.POSIXlt or as.Date are since all three work with time differently. If anyone knows of issues that could arise from using one of these other functions, I'd love to know.

> as.POSIXct("2016-05-01")
[1] "2016-05-01 EDT"
> unclass(as.POSIXct("2016-05-01"))
[1] 1462075200
attr(,"tzone")
[1] ""
> as.POSIXlt("2016-05-01")
[1] "2016-05-01 EDT"
> unclass(as.POSIXlt("2016-05-01"))
$sec
[1] 0

$min
[1] 0

$hour
[1] 0

$mday
[1] 1

$mon
[1] 4

$year
[1] 116

$wday
[1] 0

$yday
[1] 121

$isdst
[1] 1

$zone
[1] "EDT"

$gmtoff
[1] NA
> as.Date("2016-05-01")
[1] "2016-05-01"
> unclass(as.Date("2016-05-01"))
[1] 16922

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant