I'm trying to count the number of app screen-views for a particular screen using the Google Analytics BigQuery data export. My approach would be to count the number of hits with a screen-view hits.type. For instance, to count the number of page-views on the web version of our app I would count the number of hits with hits.type = 'PAGE'. but I can't see how to do this on app because there is no "SCREENVIEW" hits.type value.
This is the description of hits.type from Google (https://support.google.com/analytics/answer/3437719?hl=en):
The type of hit. One of: "PAGE", "TRANSACTION", "ITEM", "EVENT",
"SOCIAL", "APPVIEW", "EXCEPTION".
Is there another way to do this that I'm missing?
I've tried using the totals.screenviews metric:
SELECT
hits.appInfo.screenName,
SUM(totals.screenviews) AS screenViews
FROM (TABLE_DATE_RANGE([tableid.ga_sessions_], TIMESTAMP('2018-01-12'), TIMESTAMP('2018-01-12') ))
GROUP BY
hits.appInfo.screenName
But that returns numbers that are too high.
Legacy SQL automatically unnest your data which explains why your SUM(totals.screenviews) ends up being much higher (basically this field gets duplicated).
I'd recommend solving this one in Standard SQL, it's much easier and faster. See if this works for you:
#standardSQL
SELECT
name,
SUM(views) views
FROM(
SELECT
ARRAY(SELECT AS STRUCT appInfo.screenName name, COUNT(1) views FROM UNNEST(hits) WHERE type = 'APPVIEW' GROUP BY 1) data
FROM `projectId.datasetId.ga_sessions_*`
WHERE TRUE
AND EXISTS(SELECT 1 FROM UNNEST(hits) WHERE type = 'APPVIEW')
AND _TABLE_SUFFIX BETWEEN('20180112') AND ('20180112')
), UNNEST(data)
GROUP BY 1
ORDER BY 2 DESC
The hit.type is ‘APPVIEW’, because it no counts events.
#standardSQL
SELECT
hit.appInfo.screenName name,
count(hit.appInfo.screenName) view
FROM
project_id.dataset_id.ga_sessions_*,
UNNEST(hits) hit
WHERE type = 'APPVIEW'
GROUP BY
name)
Related
I started exploring big query, i am wondering, is it possible to combine in big query or GA number of unique users and pages that they have seen?
So i want to see how many are there Y unique visitors who viewed one or more pages and of these, Z% also viewed W pages?
I used below query to get Y unique visitors who viewed certain pages, but not able to see the % who have viewed W pages.
#standardSQL
SELECT
hits.page.pagePath AS other_seen_pages,
COUNT(hits.page.pagePath) AS number_other_seen_pages
FROM `project.dataset.session`,UNNEST(hits) AS hits
WHERE fullVisitorId IN (
SELECT fullVisitorId
FROM `project.dataset.session`,UNNEST(hits) AS hits
WHERE hits.page.pagePath LIKE '%x_page%'
GROUP BY fullVisitorId )
AND hits.page.pagePath IS NOT NULL
AND hits.page.pagePath NOT LIKE '%x_page%'
GROUP BY other_seen_pages
ORDER BY number_other_seen_pages DESC;
I understand that you would like a query where, on top on the other pages that the same visitors visited, the number of visitors (from the same subset of visitors) that visited them (and the percentage above the total amount of users) appears.
Here is some code that worked for me with the bigquery-public-data.google_analytics_sample.ga_sessions_20170801 Google Analytics public table and the '/google+redesign/electronics' pagePath:
It:
creates a table with the total number of different users in the table
creates a table like yours, with the addition of a filed for the total of different users that visited your page and the page of the row
selects the desired fields from these two tables and computes the %
.
WITH
t_total_users as (select count(DISTINCT fullVisitorId) as total_users from `bigquery-public-data.google_analytics_sample.ga_sessions_20170801`),
t_other_pages as (SELECT
hits.page.pagePath AS other_seen_pages,
COUNT(hits.page.pagePath) AS number_other_seen_pages,
COUNT(DISTINCT fullvisitorID) as visitors_per_page
FROM `bigquery-public-data.google_analytics_sample.ga_sessions_20170801`, UNNEST(hits) AS hits
WHERE fullVisitorId IN (
SELECT fullVisitorId
FROM `bigquery-public-data.google_analytics_sample.ga_sessions_20170801`,UNNEST(hits) AS hits
WHERE hits.page.pagePath LIKE '/google+redesign/electronics'
GROUP BY fullVisitorId )
AND hits.page.pagePath IS NOT NULL
AND hits.page.pagePath NOT LIKE '/google+redesign/electronics'
GROUP BY other_seen_pages
ORDER BY number_other_seen_pages DESC)
SELECT
t_other_pages.other_seen_pages,
t_other_pages.number_other_seen_pages,
t_other_pages.visitors_per_page,
t_total_users.total_users,
(t_other_pages.visitors_per_page/t_total_users.total_users)*100 as percentage_visitants
FROM t_total_users, t_other_pages
If there is something in the query goal I missunderstood please specify!
I apologize if this has been asked before, but I can't seem to find a specific doc describing how to do this. We are importing our GA data into big query. i simply need to see waht visitors on our site have been viewing two or more pages and completing at least one of a few actions. I am fairly new to BQ, and teh docs I have read talk of using UNNEST, unfortunately, thi sis the issue I am seeing, when i run this query:
SELECT visitId, totals.pageviews FROM `analytics-acquisition-funnel.119485123.ga_sessions_20181009` WHERE totals.pageviews > 2 AND
'modal-click' IN UNNEST(hits.eventInfo.eventAction)
order by totals.pageviews DESC LIMIT 100000
I get the following issue, shouldn't this work. I apologize, I reading this doc, but I feel like my use case is simpler than most shown:
https://cloud.google.com/bigquery/docs/reference/standard-sql/arrays#scanning-arrays
Cannot access field eventInfo on a value with type ARRAY> at [2:30]
Below is for BigQuery Standard SQL
#standardSQL
SELECT visitId, totals.pageviews
FROM `analytics-acquisition-funnel.119485123.ga_sessions_20181009`
WHERE totals.pageviews > 2
AND (SELECT COUNTIF(eventInfo.eventAction = 'modal-click') FROM UNNEST(hits)) > 0
ORDER BY totals.pageviews DESC
LIMIT 100000
OR
#standardSQL
SELECT visitId, totals.pageviews
FROM `analytics-acquisition-funnel.119485123.ga_sessions_20181009`
WHERE totals.pageviews > 2
AND EXISTS(SELECT 1 FROM UNNEST(hits) WHERE eventInfo.eventAction = 'modal-click')
ORDER BY totals.pageviews DESC
LIMIT 100000
We are validating a query in Big Query, and cannot get the results to match with the google analytics UI. A similar question can be found here, but in our case the the mismatch only occurs when we apply a specific filter on ecommerce_action.action_type.
Here is the query:
SELECT COUNT(distinct fullVisitorId+cast(visitid as string)) AS sessions
FROM (
SELECT
device.browserVersion,
geoNetwork.networkLocation,
geoNetwork.networkDomain,
geoNetwork.city,
geoNetwork.country,
geoNetwork.continent,
geoNetwork.region,
device.browserSize,
visitNumber,
trafficSource.source,
trafficSource.medium,
fullvisitorId,
visitId,
device.screenResolution,
device.flashVersion,
device.operatingSystem,
device.browser,
totals.pageviews,
channelGrouping,
totals.transactionRevenue,
totals.timeOnSite,
totals.newVisits,
totals.visits,
date,
hits.eCommerceAction.action_type
FROM
(select *
from TABLE_DATE_RANGE([zzzzzzzzz.ga_sessions_],
<range>) ))t
WHERE
hits.eCommerceAction.action_type = '2' and <stuff to remove bots>
)
From the UI using the built in shopping behavior report, we get 3.836M unique sessions with a product detail view, compared with 3.684M unique sessions in Big Query using the query above.
A few questions:
1) We are under the impression the shopping behavior report "Sessions with Product View" breakdown is based off of the ecommerce_action.actiontype filter. Is that true?
2) Is there a .totals pre-aggregated table that the UI maybe pulling from?
It sounds like the issue is that COUNT(DISTINCT ...) is approximate when using legacy SQL, as noted in the migration guide, so the counts are not accurate. Either use standard SQL instead (preferred) or use EXACT_COUNT_DISTINCT with legacy SQL.
You're including product list views in your query.
As described in https://support.google.com/analytics/answer/3437719 you need to make sure, that no product has isImpression = TRUE because that would mean it is a product list view.
This query sums all sessions which contain any action_type='2' for which all isProduct are null or false:
SELECT
SUM(totals.visits) AS sessions
FROM
`project.123456789.ga_sessions_20180101` AS t
WHERE
(
SELECT
LOGICAL_OR(h.ecommerceaction.action_type='2')
FROM
t.hits AS h
WHERE
(SELECT LOGICAL_AND(isimpression IS NULL OR isimpression = FALSE) FROM h.product))
For legacySQL you can adapt the example in the documentation.
In addition to the fact that COUNT(DISTINCT ...) is approximate when using legacy SQL, there could be sessions in which there are only non-interactive hits, which will not be counted as sessions in the Google Analytics UI but they are counted by both COUNT(DISTINCT ...) and EXACT_COUNT_DISTINCT(...) because in your query they count visit id's.
Using SUM(totals.visits) you should get the same result as in the UI because SUM does not take into account NULL values of totals.visits (corresponding to sessions in which there are only non-interactive hits).
Background
In BigQuery, I'm trying to find the number of visitors that both visit one of two pages and purchase a specific product.
When I run each of the sub-queries, the numbers match exactly what I see in Google Analytics.
However, when I join them, the number is different than what I see in GA. I've had someone bring the results of the two sub-queries into Excel and do the equivalent, and their results equal what I'm seeing in BQ.
Details
Here's the query:
SELECT
ProductSessions.date AS date,
SUM(ProductTransactions.totalTransactions) transactions,
COUNT(ProductSessions.visitId) visited_product_sessions
FROM (
SELECT
visitId, date
FROM
`103554833.ga_sessions_20170219`
WHERE
EXISTS(
SELECT 1 FROM UNNEST(hits) h
WHERE REGEXP_CONTAINS(h.page.pagePath, r"^www.domain.com/(product|product2).html.*"))
GROUP BY visitID, date)
AS ProductSessions
LEFT JOIN (
SELECT
totals.transactions as totalTransactions,
visitId,
date
FROM
`103554833.ga_sessions_20170219`
WHERE
totals.transactions IS NOT NULL
AND EXISTS(
SELECT 1
FROM
UNNEST(hits) h,
UNNEST(h.product) prod
WHERE REGEXP_CONTAINS(prod.v2ProductName, r"^Product®$"))
GROUP BY
visitId, totals.transactions,
date) AS ProductTransactions
ON
ProductTransactions.visitId = ProductSessions.visitId
WHERE ProductTransactions.visitId is not null
GROUP BY
date
ORDER BY
date ASC
I'm expecting ProductTransactions.totalTransactions to replicate the number of transactions in Google Analytics when filtered with an advanced segment of both:
Sessions include Page matching RegEx: www.domain.com/(product|product2).html.*
Sessions include Product matches exactly: Product®
However, results in BG are about 20% higher than in GA.
Why the difference?
I'm just learning BigQuery so this might be a dumb question, but we want to get some statistics there and one of those is the total sessions in a given day.
To do so, I've queried in BQ:
select sum(sessions) as total_sessions from (
select
fullvisitorid,
count(distinct visitid) as sessions,
from (table_query([40663402], 'timestamp(right(table_id,8)) between timestamp("20150519") and timestamp("20150519")'))
group each by fullvisitorid
)
(I'm using the table_query because later on we might increase the range of days)
This results in 1,075,137.
But in our Google Analytics Reports, in the "Audience Overview" section, the same day results:
This report is based on 1,026,641 sessions (100% of sessions).
There's always this difference of roughly ~5% despite of the day. So I'm wondering, even though the query is quite simple, is there any mistake we've made?
Is this difference expected to happen? I read through BigQuery's documentation but couldn't find anything on this issue.
Thanks in advance,
standardsql
Simply SUM(totals.visits) or when using COUNT(DISTINCT CONCAT(fullVisitorId, CAST(visitStartTime AS STRING) )) make sure totals.visits=1!
If you use visitId and you are not grouping per day, you will combine midnight-split-sessions!
Here are all scenarios:
SELECT
COUNT(DISTINCT CONCAT(fullVisitorId, CAST(visitStartTime AS STRING) )) allSessionsUniquePerDay,
COUNT(DISTINCT CONCAT(fullVisitorId, CAST(visitId AS STRING) )) allSessionsUniquePerSelectedTimeframe,
sum(totals.visits) interactiveSessionsUniquePerDay, -- equals GA UI sessions
COUNT(DISTINCT IF(totals.visits=1, CONCAT(fullVisitorId, CAST(visitId AS STRING)), NULL) ) interactiveSessionsUniquePerSelectedTimeframe,
SUM(IF(totals.visits=1,0,1)) nonInteractiveSessions
FROM
`project.dataset.ga_sessions_2017102*`
Wrap up:
fullVisitorId + visitId: useful to reconnect midnight-splits
fullVisitorId + visitStartTime: useful to take splits into account
totals.visits=1 for interaction sessions
fullVisitorId + visitStartTime where totals.visits=1: GA UI sessions (in case you need a session id)
SUM(totals.visits): simple GA UI sessions
fullVisitorId + visitId where totals.visits=1 and GROUP BY date: GA UI sessions with too many chances for errors and misunderstandings
After posting the question we got into contact with Google support and found that in Google Analytics only sessions that had an "event" being fired are actually counted.
In Bigquery you will find all sessions regardless whether they had an interaction or not.
In order to find the same result as in GA, you should filter by sessions with totals.visits = 1 in your BQ query (totals.visits is 1 only for sessions that had an event being fired).
That is:
select sum(sessions) as total_sessions from (
select
fullvisitorid,
count(distinct visitid) as sessions,
from (table_query([40663402], 'timestamp(right(table_id,8)) between timestamp("20150519") and timestamp("20150519")'))
where totals.visits = 1
group each by fullvisitorid
)
The problem could be due to "COUNT DISTINCT".
According to this post:
COUNT DISTINCT is a statistical approximation for all results greater than 1000
You could try setting an additional COUNT parameter to improve accuracy at the expense of performance (see post), but I would first try:
SELECT COUNT( CONCAT( fullvisitorid,'_', STRING(visitid))) as sessions
from (table_query([40663402], 'timestamp(right(table_id,8)) between
timestamp("20150519") and timestamp("20150519")'))
What worked for me was this:
SELECT count(distinct sessionId) FROM(
SELECT CONCAT(clientId, "-", visitNumber, "-", date) as sessionId FROM `project-id.dataset-id.ga_sessions_*`
WHERE _table_suffix BETWEEN "20191001" AND "20191031" AND totals.visits = 1)
The explanation (found very well written in
this article: https://adswerve.com/blog/google-analytics-bigquery-tips-users-sessions-part-one/) is that when counting and dealing with sessions we should be careful because by default, Google Analytics breaks sessions that carryover midnight (time zone of the view). Therefore a same session can end up in two daily tables:
Image from article mentioned above
The code provided creates a sessionID by combining:
client id + visit number + date
while acknowledging the session break; the result will be in a human-readable format. Finally to match sessions in the Google Analytics UI, make sure to filter to only those with totals.visits = 1.