BigQuery & Firebase - how to determine weekly growth - firebase

I'm trying to build a dashboard for following key metrics of my Android application. To do so, I am using Firebase analytics backed by BigQuery.
I'm trying to get weekly growth of first_open event count and ratio for
the current week
the previous week
the best week ever
I'm able to get the current week and previous week first_open event count as separate queries in BigQuery (such as the following as an example for the previous week query) :
SELECT
FORMAT_DATE("%Y%m%d", DATE_SUB(CURRENT_DATE(), INTERVAL CAST ( FORMAT_DATE("%u", CURRENT_DATE()) as INT64 ) + 6 DAY)) AS previousMonday,
FORMAT_DATE("%Y%m%d", DATE_SUB(CURRENT_DATE(), INTERVAL CAST ( FORMAT_DATE("%u", CURRENT_DATE()) as INT64 ) DAY)) AS previousSunday,
COUNT(*) as counter,
h.name as event
FROM `com_package_app_ANDROID.app_events_*`, UNNEST(event_dim) as h
WHERE _TABLE_SUFFIX
BETWEEN
FORMAT_DATE("%Y%m%d", DATE_SUB(CURRENT_DATE(), INTERVAL CAST ( FORMAT_DATE("%u", CURRENT_DATE()) as INT64 ) + 6 DAY))
AND
FORMAT_DATE("%Y%m%d", DATE_SUB(CURRENT_DATE(), INTERVAL CAST ( FORMAT_DATE("%u", CURRENT_DATE()) as INT64 ) DAY))
AND
h.name='first_open'
GROUP BY event
ORDER BY counter DESC
But I'm unable to get a ratio by combining the 2 queries (for current week vs previous week), and also, I'm unable to get the best ever week first_open count.

To get the "best week ever", you can group the events by week and order by count. Something like this...
SELECT
DATE_TRUNC(event_date, WEEK) AS week,
COUNT(*) AS count
FROM BQ_TABLE
WHERE BQ_TABLE.name = 'first_open'
GROUP BY week
ORDER BY count DESC
To get the current/previous week, you can turn the above query into a subquery and filter it by your target weeks.
Note that the "best week ever" query will always be a full table scan and could get expensive depending on the number of events and how frequently you need to perform the query.

Related

How to get previous month number in SQLite

How to get Previous Month number and Month before Previous Month number in SQLite,
I am using strftime('%m','now') for getting current month number but not finding anything to extract previous month number, how to do this
You can use the date function to get the previous month:
SELECT strftime('%m', date('now','start of month','-1 month'));
SELECT strftime('%m', date('now','start of month','-2 month'));
It's fairly self explanatory: This uses the date function to get the first day of the current month (to prevent issues on the last days of a month with more days than the previous month), then subtract 1 or 2 months. Then use strftime to pull out just the month number.
SQLite has a modulo operator (%), so if month is the current month number, you can calculate:
previous_month = (month - 2) % 12 + 1
month_before_previous = (month - 3) % 12 + 1

BigQuery data matching GA on daily basis but not over larger time frame

I am calculating Bounced sessions (sessions with only 1 pageview) via BQ.
Query is joining a table that gives me number of all sessions and a table that gives me bounced sessions.
When I run my query on just one specific date, my numbers match with the numbers in GA, but if I select bigger timeframe, for example a month, the numbers (only for Bounced sessions) are off.
Also, if I run each subquery separately, I get correct numbers for any timeframe.
Here is my query:
SELECT
A.date AS Date,
A.Landing_Content_Group AS Landing_Content_Group,
MAX(A.sessions) AS Sessions,
MAX(B.Bounced_Sessions) AS Bounced_Sessions
FROM (
SELECT
date,
hits.contentGroup.contentGroup2 AS Landing_Content_Group,
COUNT(DISTINCT CONCAT(CAST(visitStartTime AS string),fullVisitorId)) AS sessions
FROM
`122206032.ga_sessions_201808*`,
UNNEST(hits) AS hits
WHERE
hits.type="PAGE"
AND hits.isEntrance = TRUE
GROUP BY
date,
Landing_Content_Group
ORDER BY
date DESC,
sessions DESC ) A
LEFT JOIN (
SELECT
date,
hits.contentGroup.contentGroup2 AS Landing_Content_Group,
COUNT(DISTINCT CONCAT(CAST(visitStartTime AS string),fullVisitorId)) AS Bounced_Sessions
FROM
`122206032.ga_sessions_201808*`,
UNNEST(hits) AS hits
WHERE
hits.type="PAGE"
AND totals.pageviews = 1
AND hits.isEntrance = TRUE
GROUP BY
date,
Landing_Content_Group
ORDER BY
date DESC,
Bounced_Sessions DESC ) B
ON
a.Landing_Content_Group = b.Landing_Content_Group
GROUP BY
Date,
Landing_Content_Group
ORDER BY
Date DESC,
Sessions DESC
What I should get:
GA results
What I get in BQ for that date when a time frame is a month:
BQ results
I tried different JOINs and Aggregations but so far still in the unknown :)
Ok, I solved it, the solution was to also join the tables on the date.
ON
a.date = b.date
AND a.Landing_Content_Group = b.Landing_Content_Group

Group by month Google Analytics / BigQuery

I am pretty new to BigQuery and have a question about grouping the Date using Google Analytics data (StandardSQL). The data is currently on daily level, how can I aggregate this to Year/Month level?
Desired outcome: Data on year/month level + selection of only the last 12 months.
#StandardSQL
SELECT
TIMESTAMP(PARSE_DATE('%Y%m%d',date)) as Date,
SUM(totals.visits) AS Visits,
totals.timeOnSite AS TimeOnSite,
totals.newVisits AS NewVisit
FROM
`XXXX.ga_sessions_20*`
WHERE
_TABLE_SUFFIX >= '180215'
GROUP by
Date,
TimeOnSite,
NewVisit
Thanks in advance!
As you limit the data selection to the previous year and if you have a field in your database that registers the date of the visit, you can get your aggregated results per month using this query:
#StandardSQL
SELECT
EXTRACT(MONTH FROM 'date_field_of_the_visit') AS Month,
sum(totals.visits) AS Visits
FROM
'XXXX.ga_sessions_20*'
WHERE
_TABLE_SUFFIX >= '170312'
Group by Month
You can use DATE_TRUNC function (https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#date_trunc) for that:
#StandardSQL
SELECT
DATE_TRUNC(PARSE_DATE('%Y%m%d',date), MONTH) as MonthStart,
SUM(totals.visits) AS Visits,
totals.timeOnSite AS TimeOnSite,
totals.newVisits AS NewVisit
FROM
`XXXX.ga_sessions_20*`
WHERE
_TABLE_SUFFIX >= '180215'
GROUP by
Date,
TimeOnSite,
NewVisit

How to pull data from the past 3 days, not including today's and yesterday's from Bigquery

Using standars SQL. When the table consists of wildcard tables.
I Try to use this:
SELECT *
FROM `Resourse.Reports_Reg.Session_streaming`
WHERE
SUBSTR(_table_suffix, 0, 6) =
FORMAT_DATE("%E4Y%m", DATE_SUB(CURRENT_DATE, INTERVAL 3 day))
This request, I thought, should return data for the last 5 days. But I still do not understand what data it returns to me. How to pull data from the past 3 days, not including today's and yesterday's
As it is, you are not selecting anything into _table_suffix given that your query has no wildcard.
If your table have for instance a name structure like:
`Resourse.Reports_Reg.Session_streaming_20170820`
where the very last string is a date with format "%Y%m%d", then selecting past third and second days can be done like:
SELECT
*
FROM `Resourse.Reports_Reg.Session_streaming_*`
WHERE _TABLE_SUFFIX BETWEEN FORMAT_DATE("%Y%m%d", DATE_SUB(CURRENT_DATE, INTERVAL 3 day)) AND FORMAT_DATE("%Y%m%d", DATE_SUB(CURRENT_DATE, INTERVAL 2 day))
Notice the wildcard "*" is selecting the date "20170820" for instance. After that, there's just a where clause selecting appropriate dates.

SQLite : obtain current week

I'm trying to obtain the current week for date comparison in SQLite.
I have no problem for last month, last year, today, yesterday... but don't find the solution to have the current week.
I tried lot of things like:
SELECT tastings.* FROM tastings
WHERE (DATE(tastings.date) > DATE('now','weekday 1','+ 7 days'))
Can you help me ? Thanks.
This code gives you the week number where the first day of week is monday. It also works well for last and first weeks of the year.
strftime('%W', 'now', 'localtime', 'weekday 0', '-6 days')
I guess you want compare 2 date, Assume you have a table named _testTbl and have 3 column _id INTEGER, _name TEXT, _recordDate TEXT
you want name that record this week
you can use below code:
SELECT * FROM _testTbl
WHERE _recordDate > datetime('now', 'start of day', 'weekday 6', '-7 day')
note that this week start by saturday (sunday 0, monday 1, ..., saturday 7)
this t-sql means:
datetime is a sqlite date and time function.
first parameter is given time: 'now' means the current time.
second parameter take the time to start of day.
third parameter take time to the next weekday number (in this case, saturday).
fourth parameter take time to start of week
What is stored inside the tastings.date column? Note that SQLite does not have “timestamp” type affinity, so probably you store Text (some representation of the date) or integer (julian day, epoch…)
All time and date functions expect a valid time string and convert that time string to another string format. If tastings.date contains a week number then use:
AND cast(tastings.date AS TEXT) = strftime('%W','now')
This helps me to compare the 2 dates using the week of the year.
AND ( strftime('%W', tastings.date) = strftime('%W', 'now') )
Thanks you.

Resources