shecode
shecode

Reputation: 1726

How to get the Google Analytics definition of unique page views in Bigquery

https://support.google.com/analytics/answer/1257084?hl=en-GB#pageviews_vs_unique_views

I'm trying to calculate the sum of unique page views per day which Google analytics has on its interface How do I get the equivalent using bigquery?

Upvotes: 4

Views: 5371

Answers (4)

Aku
Aku

Reputation: 802

So, in 2022 EXACT_COUNT_DISTINCT() seems to be deprecated..
Also for me the following combination of
fullvisitorid+visitNumber+visitStartTime+hits.page.pagePath
was always more precise than the above solutions:

SELECT 
    SUM(Unique_PageViews)
FROM
    (SELECT 
        COUNT(DISTINCT(CONCAT(fullvisitorid,"-",CAST(visitNumber AS string),"-",CAST(visitStartTime AS string),"-",hits.page.pagePath))) as Unique_PageViews
    FROM 
        `mhsd-bigquery-project.8330566.ga_sessions_*`,
        unnest(hits) as hits
    WHERE 
        _table_suffix BETWEEN '20220307'
        AND '20220313' 
        AND hits.type = 'PAGE')

Upvotes: 0

bunchan
bunchan

Reputation: 11

The other queries didn't match the Unique Pageviews metric in my Google Analytics account, but the following did:

SELECT COUNT(1) as unique_pageviews
FROM (
    SELECT 
        hits.page.pagePath, 
        hits.page.pageTitle,
        fullVisitorId,
        visitNumber,
        COUNT(1) as hits
    FROM [my_table]
    WHERE hits.type='PAGE' 
    GROUP BY 
        hits.page.pagePath, 
        hits.page.pageTitle,
        fullVisitorId,
        visitNumber
)

Upvotes: 1

Thunder Storm
Thunder Storm

Reputation: 121

For uniquePageViews you better want to use something like this:

SELECT
  date,
  SUM(uniquePageviews) AS uniquePageviews
FROM (
  SELECT
    date,
    CONCAT(fullVisitorId,string(VisitId)) AS combinedVisitorId,
    EXACT_COUNT_DISTINCT(hits.page.pagePath) AS uniquePageviews
  FROM
    [google.com:analytics-bigquery:LondonCycleHelmet.ga_sessions_20130910]
  WHERE
    hits.type='PAGE'
  GROUP BY 1,2)
GROUP EACH BY 1;

Upvotes: 0

Pentium10
Pentium10

Reputation: 207912

There are two ways how this is used:

1) One is as the original linked documentation says, to combine full visitor user id, and their different session id: visitId, and count those.

SELECT
  EXACT_COUNT_DISTINCT(combinedVisitorId)
FROM (
  SELECT
    CONCAT(fullVisitorId,string(VisitId)) AS combinedVisitorId
  FROM
    [google.com:analytics-bigquery:LondonCycleHelmet.ga_sessions_20130910]
  WHERE
    hits.type='PAGE' )

2) The other is just counting distinct fullVisitorIds

SELECT
  EXACT_COUNT_DISTINCT(fullVisitorId)
FROM
  [google.com:analytics-bigquery:LondonCycleHelmet.ga_sessions_20130910]
WHERE
  hits.type='PAGE'

If someone wants to try out this on a sample public dataset there is a tutorial how to add the sample dataset.

Upvotes: 3

Related Questions