Change dates dynamically to fetch data from google calendar api - google-calendar-api

I'm currently fetching data from google calendar freebusy endpoint: https://www.googleapis.com/calendar/v3/freeBusy with the following request object:
{
"timeMin": "2021-12-31T10:30:00.000z",
"timeMax": "2022-01-12T09:30:00.000z",
"calendarExpansionMax": 11,
"groupExpansionMax": 11,
"items": [
{
"id": "ali.mojiz1#gmail.com"
}
]
}
I want to change the dates dynamically in the aforementioned object so that I can get data from the last and next 30 days based on current date for e.g. timeMin changes to 22nd December'21 and timeMax changes 22nd Feb'22.
I've tried the following to get data for the next 30 days from today but it doesn't work:
{
"timeMin": "2022-01-22T10:30:00.000z",
"timeMax": "{date.add(30).format('yyyy-MM-ddT23:59:59Z')}",
"calendarExpansionMax": 11,
"groupExpansionMax": 11,
"items": [
{
"id": "ali.mojiz1#gmail.com"
}
]
}
Any help would be appreciated. Thanks!

the following goes back to 30 days from today for e.g. today's 25th Jan'22 so this will give you 25th Dec'21.
"timeMin": {
"function": "concat",
"args": [
{
"function": "date",
"args": [
"Y-m-d",
{
"function": "strtotime",
"args": [
"-30 days",
{
"function": "time"
}
]
}
]
},
"T00:00:00-04:00"
]

Related

Google Calendar API all day event spanning an extra day?

In my Google calendar, which is in the America/Toronto timezone, I have an event that is marked as an all day on the 13 April 2022, but it is coming back as starting on 2022-04-13 and ending on 2022-04-14. I have some other all day event behaving in the same way.
This is happening from my code, but also when I try with the API test form here:
https://developers.google.com/calendar/api/v3/reference/events/list
With parameters:
timeMin: 2022-04-10T18:36:39.000Z
timeMax: 2022-04-17T18:36:39.000Z
This returns me the following response (certain fields masked):
"kind": "calendar#events",
"etag": "\"p328ftm5rim7ve0g\"",
"summary": "My Test Calendar",
"updated": "2022-04-12T18:36:40.074Z",
"timeZone": "America/Toronto",
"accessRole": "owner",
"defaultReminders": [],
"nextSyncToken": "xxx-xxxx_cCEJD-2LuVj_cCGAUg8t3t0QE=",
"items": [
{
"kind": "calendar#event",
"etag": "\"3299577200148000\"",
"id": "xxxxga02nqb11ub6dm29dgg9t",
"status": "confirmed",
"htmlLink": "https://www.google.com/calendar/event?eid=xxxxxxnYTAybnFiMTF1YjZkbTI5ZGdnOXQgY19uZ2hqOXFzb25tYjRtNDRqN2doaTNxNzY5a0Bn&ctz=UTC",
"created": "2022-04-12T18:36:39.000Z",
"updated": "2022-04-12T18:36:40.074Z",
"summary": "Dummy Event",
"creator": {
"email": "xxxxxx",
"displayName": "xxxxxx"
},
"organizer": {
"email": "xxxxxxx#group.calendar.google.com",
"displayName": "My Test Calendar",
"self": true
},
"start": {
"date": "2022-04-13"
},
"end": {
"date": "2022-04-14"
},
"transparency": "transparent",
"iCalUID": "xxxxx#google.com",
"sequence": 0,
"reminders": {
"useDefault": false
},
"eventType": "default"
}
]
}
I tried providing both 'America/Toronto' and 'UTC' as timeZone parameters in the request, but this does not change anything.
Can anyone suggest how to get an all day event to span only the day it is meant to be happening on?
BTW below you can see the event was only on a single day:
This is an expected behavior from the Calendar API. All day event last for 24 hours. Lasting from midnight of start date to midnight of the next day.
It is also written in the Event Resource that end is exclusive.
An alternative solution to this is to -1 the end date of the response body or manually plot the time. But if you try to manually create an All day event by plotting the time, It can only last for 23.5 hrs.

Google Calendar FreeBusy API not showing empty time slots

I have events from:
11:30 AM to 1:30 PM
3:45 PM to 5:45 PM
7:00 PM to 12:00 AM
I am trying to get all the empty calendar slots in within the timeMin and timeMax duration.
Yet when I do at freebusy API:
{
"timeMin": "2021-03-25T11:42:20.698908-04:00",
"timeMax": "2021-03-25T22:42:20.698908-04:00",
"calendarExpansionMax": 10 #doesn't make any difference
}
I get:
{
"kind": "calendar#freeBusy",
"timeMin": "2021-03-25T15:42:20.000Z",
"timeMax": "2021-03-26T02:42:20.000Z"
}
which I don't know what it even represents nor does it make it clear. I am not sure why it's even going post the timeMax duration I allowed by going to the next day (26th).
If the freebusy api isn't the solution, has anyone built a solution for it in python, as there are many SO answers and libs for javascript.
You have missing parameter item[] in your request. This is where you put the calendar id which the request will do its query.
Your request parameter should look like this:
{
"timeMin": "2021-03-25T11:42:20.698908-04:00",
"timeMax": "2021-03-25T22:42:20.698908-04:00",
"timeZone": "UTC-4",
"items": [
{
"id": "insert the calendar id here"
}
]
}
Output:
{
"kind": "calendar#freeBusy",
"timeMin": "2021-03-25T03:42:20.000Z",
"timeMax": "2021-03-25T14:42:20.000Z",
"calendars": {
"calendar id here": {
"busy": [
{
"start": "2021-03-25T11:42:20+08:00",
"end": "2021-03-25T13:30:00+08:00"
},
{
"start": "2021-03-25T15:45:00+08:00",
"end": "2021-03-25T17:45:00+08:00"
},
{
"start": "2021-03-25T19:00:00+08:00",
"end": "2021-03-25T22:42:20+08:00"
}
]
}
}
}
Since it only return the busy time range, you have to calculate the free time in your script.

HERE REST API (fleet.ls) Timezone return incorrect for Adelaide (Australia) possible half hour confuses API

HERE REST API fleet.ls
Consider the following REST API call. Note the Long Lats are in Adelaide Australia which has a TZ of +9:30.
https://fleet.ls.hereapi.com/2/calculateroute.json?waypoint0=-34.8751,138.5276&waypoint1=-34.9042,138.5708;sort&waypoint2=stopOver,600!-34.893,138.5546;sort&departure=2021-01-08T17:15:00&mode=fastest;car;traffic:enabled&legAttributes=-li,-mn,le,bt,tt,-tm,sh&routeAttributes=sm,wp&apikey=xxxxxx
The Departure time is set to:
departure=2021-01-08T17:15:00
However the summary returns the following:
"summary": {
"travelTime": 1010,
"distance": 5102,
"baseTime": 882,
"trafficTime": 1010,
"flags": [],
"departure": "2021-01-08T**17:15:00+10**",
"arrival": "2021-01-08T17:31:49+10"
}
The absolute time is incorrect as the location has a timezone of +9.5 (or +10.5 DST). This, is then passed through to other algorithms used and, well, messes everything up by a half an hour.
It appears you are using version 7 of the Calculate Route API, as shown here. It shows timestamps with only whole-hour offsets, such as 2013-07-04T17:00:00+02.
The docs here for version 8+ of the API show a very different format for its output, including timestamps with full hours and minutes in the offset, such as 2019-12-09T16:05:05+01:00. The full example in docs is:
{
"routes": [
{
"id": "bfaed7d0-19c7-4e72-81b7-24eeb148b62b",
"sections": [
{
"arrival": {
"place": {
"location": {
"lat": 52.53232637420297,
"lng": 13.378873988986015
},
"type": "place"
},
"time": "2019-12-09T16:05:05+01:00"
},
"departure": {
"place": {
"location": {
"lat": 52.53098367713392,
"lng": 13.384566977620125
},
"type": "place"
},
"time": "2019-12-09T16:03:02+01:00"
},
"id": "85357f8f-00ad-447e-a510-d8c02e0b264f",
"summary": {
"duration": 123,
"length": 538
},
"transport": {
"mode": "car"
},
"type": "vehicle"
}
]
}
]
}
I suggest you use the latest v8 of the API (8.14.0 at time of writing this). It should give the correct offsets for Adelaide.

Extract member info from Google Analytics

I am trying to get all data from my Google Analytics account to a database using their Reporting API v4. In the below request and response, I see a number in the values attribute. I want to get member specific data for that number? For example, value is 65 for dimension 20130101. I want to get data of those 65 members that accessed my website that day (2013-01-01 - the specified dimension in the request). Any data that GA has will suffice. For example, the Gender of those 65 members, their session Id, age etc.
Google Analytics Reporting API Request:
{
"reportRequests": [
{
"viewId": "345",
"dateRanges": [
{
"startDate": "daysAgo",
"endDate": "yesterday"
}
],
"metrics": [
{
"expression": "ga:users"
}
],
"dimensions":[
{
"name":"ga:date"
}
]
}
]
}
Response
{
"reports": [
{
"columnHeader": {
"dimensions": [
"ga:date"
],
"metricHeader": {
"metricHeaderEntries": [
{
"name": "ga:users",
"type": "INTEGER"
}
]
}
},
"data": {
"rows": [
{
"dimensions": [
"20130101"
],
"metrics": [
{
"values": [
"65"
]
}
]
},
{
"dimensions": [
"20130102"
],
"metrics": [
{
"values": [
"69"
]
}
]
},
{
"dimensions": [
"20130103"
],
"metrics": [
{
"values": [
"48"
]
}
]
}
],
"totals": [
{
"values": [
"490"
]
}
],
"rowCount": 3,
"minimums": [
{
"values": [
"44"
]
}
],
"maximums": [
{
"values": [
"94"
]
}
],
"isDataGolden": true
}
}
]
}
Google Analytics reporting api don't provide single rows reports, that means that all the data that you can collect is gruped by the dimension.
In that case, if you want to retrieve al this data in a single report, you have to implement a custom dimension to a users level to identify each one, one option is send the client id (the _ga cookie) to the platform and add that dimension
Here is a good post of how to implement this
https://www.simoahava.com/analytics/add-clientid-to-custom-dimension-gtag-js/
Have in mind that you can expect others on the reports in that case, due the high cardinality.
https://support.google.com/analytics/answer/1009671?hl=en
For premium users you can export the data via BigQuery
Greetings

Google Calendar Events after 7pm are not retrieved until the Next Day using Google Events API

I've updated this because this question makes more sense than the original. I'm still including the test code that I used to show what's happening.
Please see the comments for more up to date information on what I've found.
Executed API to grab events for today (there should be 1):
/calendar/v3/calendars/{calid}/events?calendarId={calid}&singleEvents=true&timeMin=2013-04-24T00:00:00.000Z&timeMax=2013-04-25T00:00:00.000Z
It retrieves nothing. No events are returned.
If I execute the following (ie, the next day):
/calendar/v3/calendars/{calid}/events?calendarId={calid}&singleEvents=true&timeMin=2013-04-25T00:00:00.000Z&timeMax=2013-04-26T00:00:00.000Z
It retrieves the following which is set for the 24th (yet after 7pm), not the 25th:
{
"kind": "calendar#events",
"etag": "\"GZxpEFttRDAOmLHnWRxLHHWPGwk/01XVNYQjwJ5jTmd05uIgK9e6Uhw\"",
"summary": "test calendar",
"description": "test calendar",
"updated": "2013-04-24T13:09:12.000Z",
"timeZone": "America/Chicago",
"items": [
{
"kind": "calendar#event",
"etag": "\"GZxpEFttRDAOmLHnWRxLHHWPGwk/Z2NhbDAwMDAxMzY2MTQ4ODczODI3MDAw\"",
"id": "d1mdj3dasor22f0nm0lbohru7s",
"status": "confirmed",
"htmlLink": "https://www.google.com/calendar/event?eid=ZDFtZGozZGFzb3IyMmYwbm0wbGJvaHJ1N3MgYnZzdG9vbHMuY29tX2xidWt1ZmlnczJjMmFycjViODgycDVhYWhvQGc",
"created": "2013-04-16T21:47:53.000Z",
"updated": "2013-04-16T21:47:53.827Z",
"summary": "this should happen 4/24/2013",
"description": "testing event",
"creator": {
"email": "{my email address}",
"displayName": "my name"
},
"organizer": {
"email": "{calid}",
"displayName": "test calendar",
"self": true
},
"start": {
"dateTime": "2013-04-24T21:21:19-05:00"
},
"end": {
"dateTime": "2013-04-24T21:21:19-05:00"
},
"iCalUID": "d1mdj3dasor22f0nm0lbohru7s#google.com",
"sequence": 0,
"extendedProperties": {
"private": {
"evtKey": "key"
}
},
"reminders": {
"useDefault": true
}
}
]
}
It should be retrieving it for the first api call on the 24th, not the 2nd api call on the 25th unless I'm missing something.
Any ideas? Thanks!
a bit late but if you still needs it. I think the event you retrieved d1mdj3dasor22f0nm0lbohru7 seems to be in default timezone of the calendar, which is America/Chicago. That explains why it only showed when you set your query to 25th UTC time

Resources