4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

BigQueryでGoogle Analyticsのデータを整理して取り出す

Last updated at Posted at 2019-01-18

欲しいデータ

BigQueryにインポートされているGoogle Analyticsのデータを整理して取り出したい。
データのスキーマ:https://support.google.com/analytics/answer/3437719

サブクエリをひとつずつメモ

(1) データ取得開始日、終了日を定義

CREATE TEMPORARY FUNCTION START_DATE()  AS ('20181216');
CREATE TEMPORARY FUNCTION END_DATE()  AS ('20181223');

(2) 除外したいユーザーリストを作成
pagePathに特定の単語を含むユーザーをリストアップ

WITH 
## 除外ユーザーリストを作成
ignore_users AS(
    SELECT 
        fullVisitorId 
    FROM `<BigQueryのテーブル名>`,unnest(hits) AS hits 
        WHERE _TABLE_SUFFIX BETWEEN START_DATE() AND END_DATE()
            AND hits.page.pagePath LIKE '%XXXXXX%'  # pagePath'XXXXX'を含む人を除外したい
            OR  hits.page.pagePath LIKE '%YYYYYY%'  # pagePath'YYYYY'を含む人を除外したい
            OR  hits.page.pagePath LIKE '%ZZZZZZ%'  # pagePath'ZZZZZ'を含む人を除外したい
        GROUP BY fullVisitorId
),

(2) (1)で作成したユーザーを除外、pagePathからパラメーターを削除
'?'が出現したらpagePathを切り取り、0番目の項目のみを残す
例:https://qiita.com?id=xxxxxxxhttps://qiita.com

## 特定ユーザーを除外、pagePathのパラメータを削除
delete_parameter AS(
    SELECT 
        fullVisitorId,
        SPLIT(hits.page.pagePath, '?')[OFFSET(0)] AS pagePath #URLのパラメーターを削除
    FROM `<BigQueryのテーブル名>`,unnest(hits) AS hits 
      WHERE _TABLE_SUFFIX BETWEEN START_DATE() AND END_DATE() 
      AND hits.type = 'PAGE'  
      AND fullVisitorId NOT IN(SELECT fullVisitorId FROM ignore_users) #特定ユーザーを除外
    GROUP BY fullVisitorId, pagePath
),

(3) パラメーター削除後のページでPV数がTOP200のページを抽出

## PV数がTOP200のページを抽出
page_extract AS(
    SELECT 
        pagePath,
        COUNT(*) AS pagePathCount
    FROM delete_parameter
    GROUP BY pagePath
    ORDER BY pagePathCount DESC
    LIMIT 200
),

(4) 対象ユーザーと対象pagePathでtransactionデータを作成
直帰ユーザーを取り除く用にユーザーごとのページ閲覧数をcountしておく

## 対象ユーザーと対象pagePathtransactionデータを作成
## ユーザーごとのページ閲覧数をcount
count_pv AS(
SELECT
    fullVisitorId,
    pagePath,
    COUNT(pagePath) OVER (PARTITION BY fullVisitorId) AS countPagePerUser
FROM delete_parameter 
WHERE pagePath IN(SELECT pagePath FROM page_extract)
)

(5) 直帰ユーザーを除き、データを整形


## 2ページ以上見たuserのデータのみを抽出
SELECT
    fullVisitorId,
    pagePath
FROM count_pv
WHERE countPagePerUser >= 2
ORDER BY fullVisitorId

まとめると

CREATE TEMPORARY FUNCTION START_DATE()  AS ('20181216');
CREATE TEMPORARY FUNCTION END_DATE()  AS ('20181223');

WITH 
## 除外ユーザーリストを作成
ignore_users AS(
    SELECT 
        fullVisitorId 
    FROM `<BigQueryのテーブル名>`,unnest(hits) AS hits 
        WHERE _TABLE_SUFFIX BETWEEN START_DATE() AND END_DATE()
            AND hits.page.pagePath LIKE '%XXXXXX%'  # pagePath'XXXXX'を含む人を除外したい
            OR  hits.page.pagePath LIKE '%YYYYYY%'  # pagePath'YYYYY'を含む人を除外したい
            OR  hits.page.pagePath LIKE '%ZZZZZZ%'  # pagePath'ZZZZZ'を含む人を除外したい
        GROUP BY fullVisitorId
),

## 特定ユーザーを除外、pagePathのパラメータを削除
delete_parameter AS(
    SELECT 
        fullVisitorId,
        SPLIT(hits.page.pagePath, '?')[OFFSET(0)] AS pagePath #URLのパラメーターを削除
    FROM `<BigQueryのテーブル名、(1)のテーブル名と一緒>`,unnest(hits) AS hits 
      WHERE _TABLE_SUFFIX BETWEEN START_DATE() AND END_DATE() 
      AND hits.type = 'PAGE'  
      AND fullVisitorId NOT IN(SELECT fullVisitorId FROM ignore_users)
    GROUP BY fullVisitorId, pagePath
),

## PV数がTOP200のページを抽出
page_extract AS(
    SELECT 
        pagePath,
        COUNT(*) AS pagePathCount
    FROM delete_parameter
    GROUP BY pagePath
    ORDER BY pagePathCount DESC
    LIMIT 200
),

## 対象ユーザーと対象pagePathtransactionデータを作成
## ユーザーごとのページ閲覧数をcount
count_pv AS(
SELECT
    fullVisitorId,
    pagePath,
    COUNT(pagePath) OVER (PARTITION BY fullVisitorId) AS countPagePerUser
FROM delete_parameter 
WHERE pagePath IN(SELECT pagePath FROM page_extract)
)

## 2ページ以上見たuserのデータのみを抽出
SELECT
    fullVisitorId,
    pagePath
FROM count_pv
WHERE countPagePerUser >= 2
ORDER BY fullVisitorId

大量のデータをぱぱっと整理してくれてBigQueryにはいつも感謝です!!ありがとう!!

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?