|
12 | 12 | ) }} |
13 | 13 |
|
14 | 14 | -- https://support.google.com/firebase/answer/7029846 |
15 | | -SELECT TIMESTAMP_MICROS(event_timestamp) as event_ts |
| 15 | +SELECT |
| 16 | + project_id, |
| 17 | + dataset_id, |
| 18 | + TIMESTAMP_MICROS(event_timestamp) as event_ts |
16 | 19 | , DATE(TIMESTAMP_MICROS(event_timestamp)) as event_date |
17 | 20 | , TIMESTAMP_MICROS(user_first_touch_timestamp) as install_ts |
18 | 21 | , {{ overbase_firebase.calculate_age_between_timestamps("TIMESTAMP_MICROS(event_timestamp)", "TIMESTAMP_MICROS(user_first_touch_timestamp)") }} as install_age |
@@ -57,22 +60,64 @@ SELECT TIMESTAMP_MICROS(event_timestamp) as event_ts |
57 | 60 | ) AS users_ltv |
58 | 61 | , STRUCT<firebase_app_id STRING, stream_id STRING, advertising_id STRING>( |
59 | 62 | LOWER(app_info.firebase_app_id), LOWER(stream_id), LOWER({{ null_if_length_zero('device.advertising_id') }}) |
| 63 | + ) as other_ids |
60 | 64 | , {{ overbase_firebase.generate_date_timezone_struct('TIMESTAMP_MICROS(event_timestamp)') }} as event_dates |
61 | 65 | , {{ overbase_firebase.generate_date_timezone_struct('TIMESTAMP_MICROS(user_first_touch_timestamp)') }} as install_dates |
62 | 66 | , COUNT(1) OVER (PARTITION BY user_pseudo_id, event_bundle_sequence_id, event_name, event_timestamp, event_previous_timestamp) as duplicates_cnt |
63 | | --- FROM {{ source("firebase_analytics", "events") }} as events |
64 | 67 | FROM |
65 | 68 | ( |
66 | | -{% set projects = var('OVERBASE:SOURCES', []) %} |
| 69 | +{%- set projects = var('OVERBASE:SOURCES', []) -%} |
| 70 | +{%- set ready = var('OVERBASE:SOURCES_READY', false) -%} |
67 | 71 |
|
68 | | -{% for p in projects %} |
69 | | - {% if not loop.first %}UNION ALL{% endif %} |
70 | | - select |
71 | | - '{{ p.project_id }}' as project_id, |
72 | | - * |
73 | | - from {{ source('firebase_analytics__' ~ p.project_id, 'events') }} |
74 | | - WHERE {{ overbase_firebase.analyticsTableSuffixFilter() }} |
75 | | -{% endfor %} |
| 72 | + {%- set first = (projects[0] if projects and (projects[0] is mapping) else {}) -%} |
| 73 | + {%- set pid0 = first.get('project_id', 'fallback_project') -%} |
| 74 | + {%- set ads_raw0 = first.get('analytics_dataset_ids') if first.get('analytics_dataset_ids') is not none else first.get('analytics_dataset_id') -%} |
| 75 | + {%- if ads_raw0 is string -%} |
| 76 | + {%- set ds0 = ads_raw0 -%} |
| 77 | + {%- elif ads_raw0 is iterable and (ads_raw0 | length) > 0 -%} |
| 78 | + {%- set ds0 = ads_raw0[0] -%} |
| 79 | + {%- else -%} |
| 80 | + {%- set ds0 = 'fallback_dataset' -%} |
| 81 | + {%- endif -%} |
| 82 | + |
| 83 | + {%- if not ready -%} |
| 84 | + -- FALLBACK: use the single parse-safe source until generated sources are ready |
| 85 | + SELECT |
| 86 | + '{{ pid0 }}' as project_id, |
| 87 | + '{{ ds0 }}' as dataset_id, |
| 88 | + * |
| 89 | + FROM {{ source('firebase_analytics__fallback', 'events') }} |
| 90 | + WHERE {{ overbase_firebase.analyticsTableSuffixFilter() }} |
| 91 | + AND {{ overbase_firebase.analyticsDateFilterFor('DATE(TIMESTAMP_MICROS(event_timestamp))') }} |
| 92 | + {%- else -%} |
| 93 | +{%- set ns = namespace(first=true) -%} |
| 94 | +{%- for p in projects -%} |
| 95 | + {%- set pid = p.get('project_id') -%} |
| 96 | + {%- if not pid %}{% continue %}{% endif -%} |
| 97 | + {%- set ads_raw = p.get('analytics_dataset_ids') if p.get('analytics_dataset_ids') is not none else p.get('analytics_dataset_id') -%} |
| 98 | + {%- if ads_raw is string -%} |
| 99 | + {%- set ads_list = [ads_raw] -%} |
| 100 | + {%- elif ads_raw is iterable -%} |
| 101 | + {%- set ads_list = ads_raw -%} |
| 102 | + {%- else -%} |
| 103 | + {%- set ads_list = [] -%} |
| 104 | + {%- endif -%} |
| 105 | + |
| 106 | + {%- for ds in ads_list -%} |
| 107 | + {%- if pid and ds -%} |
| 108 | + {% if not ns.first %}UNION ALL{% endif %} |
| 109 | + {% set ns.first = false %} |
| 110 | + SELECT |
| 111 | + '{{ pid }}' as project_id, |
| 112 | + '{{ ds }}' as dataset_id, |
| 113 | + * |
| 114 | + FROM {{ source('firebase_analytics__' ~ pid ~ '__' ~ ds, 'events') }} |
| 115 | + WHERE {{ overbase_firebase.analyticsTableSuffixFilter() }} |
| 116 | + AND {{ overbase_firebase.analyticsDateFilterFor('DATE(TIMESTAMP_MICROS(event_timestamp))') }} |
| 117 | + {%- endif -%} |
| 118 | + {%- endfor -%} |
| 119 | +{%- endfor -%} |
| 120 | +{%- endif -%} |
76 | 121 | ) as events |
77 | 122 | LEFT JOIN {{ref('ob_iso_country')}} as country_codes |
78 | 123 | ON LOWER(events.geo.country) = LOWER(country_codes.firebase_name) |
|
0 commit comments