|
1 | | --- TODO: should we run this job with 7 day delay to make sure all data landed (a wider window to be on the safe side). |
2 | | -WITH views_data AS ( |
| 1 | +-- https://developer.apple.com/help/app-store-connect/view-sales-and-trends/download-and-view-reports |
| 2 | +-- "Time zone: Reports are based on Pacific Time (PT). A day includes transactions that happened from 12:00 a.m. to 11:59 p.m. PT. |
| 3 | +-- However, the `date` timestamp field appear to always show midnight meaning if we do timezone conversion |
| 4 | +-- we will end up moving all results 1 day back if we attempt conversion to UTC. |
| 5 | +-- This is why we are not doing timezone converstions here. |
| 6 | +WITH historical_store_data AS ( |
| 7 | + WITH impression_data AS ( |
| 8 | + SELECT |
| 9 | + DATE(`date`) AS `date`, |
| 10 | + territory AS country_name, |
| 11 | + -- Apple used to count pageviews from these sources as impressions as well but no longer do in the new data they're sending back |
| 12 | + -- for compatibility with the new report we only want to count impressions from specific source types? |
| 13 | + SUM( |
| 14 | + CASE |
| 15 | + WHEN source_type IN ('App Referrer', 'Unavailable', 'Web Referrer') |
| 16 | + THEN 0 |
| 17 | + ELSE impressions_unique_device |
| 18 | + END |
| 19 | + ) AS impressions, |
| 20 | + FROM |
| 21 | + `moz-fx-data-shared-prod.app_store.firefox_app_store_territory_source_type_report` |
| 22 | + WHERE |
| 23 | + DATE(`date`) = DATE_SUB(@submission_date, INTERVAL 7 DAY) |
| 24 | + AND source_type <> 'Institutional Purchase' |
| 25 | + AND app_id = 989804926 -- Filter to only include the Firefox app |
| 26 | + GROUP BY |
| 27 | + `date`, |
| 28 | + country_name |
| 29 | + ), |
| 30 | + downloads_data AS ( |
| 31 | + SELECT |
| 32 | + DATE(`date`) AS `date`, |
| 33 | + territory AS country_name, |
| 34 | + SUM(total_downloads) AS total_downloads, |
| 35 | + SUM(first_time_downloads) AS first_time_downloads, |
| 36 | + SUM(redownloads) AS redownloads, |
| 37 | + FROM |
| 38 | + `moz-fx-data-shared-prod.app_store.firefox_downloads_territory_source_type_report` |
| 39 | + WHERE |
| 40 | + DATE(`date`) = DATE_SUB(@submission_date, INTERVAL 7 DAY) |
| 41 | + AND source_type <> 'Institutional Purchase' |
| 42 | + AND app_id = 989804926 -- Filter to only include the Firefox app |
| 43 | + GROUP BY |
| 44 | + ALL |
| 45 | + ) |
3 | 46 | SELECT |
4 | 47 | DATE(`date`) AS `date`, |
5 | | - territory AS country_name, |
6 | | - SUM(impressions_unique_device) AS views, |
| 48 | + country_name, |
| 49 | + COALESCE(impressions, 0) AS impressions, |
| 50 | + COALESCE(total_downloads, 0) AS total_downloads, |
| 51 | + COALESCE(first_time_downloads, 0) AS first_time_downloads, |
| 52 | + COALESCE(redownloads, 0) AS redownloads, |
7 | 53 | FROM |
8 | | - `moz-fx-data-shared-prod.app_store.firefox_app_store_territory_source_type_report` |
9 | | - WHERE |
10 | | - DATE(`date`) = DATE_SUB(@submission_date, INTERVAL 7 DAY) |
11 | | - GROUP BY |
12 | | - `date`, |
13 | | - country_name |
| 54 | + impression_data |
| 55 | + FULL OUTER JOIN |
| 56 | + downloads_data |
| 57 | + USING (`date`, country_name) |
14 | 58 | ), |
15 | | -downloads_data AS ( |
| 59 | +app_store_data AS ( |
16 | 60 | SELECT |
17 | | - DATE(`date`) AS `date`, |
18 | | - territory AS country_name, |
| 61 | + date_day AS `date`, |
| 62 | + territory_long AS country_name, |
| 63 | + SUM(impressions_unique_device) AS impressions, |
19 | 64 | SUM(total_downloads) AS total_downloads, |
20 | 65 | SUM(first_time_downloads) AS first_time_downloads, |
21 | 66 | SUM(redownloads) AS redownloads, |
22 | 67 | FROM |
23 | | - `moz-fx-data-shared-prod.app_store.firefox_downloads_territory_source_type_report` |
| 68 | + `moz-fx-data-bq-fivetran.firefox_app_store_v2_apple_store.apple_store__territory_report` |
24 | 69 | WHERE |
25 | | - DATE(`date`) = DATE_SUB(@submission_date, INTERVAL 7 DAY) |
| 70 | + DATE(date_day) = DATE_SUB(@submission_date, INTERVAL 7 DAY) |
26 | 71 | AND source_type <> 'Institutional Purchase' |
| 72 | + AND app_id = 989804926 -- Filter to only include the Firefox app |
27 | 73 | GROUP BY |
| 74 | + ALL |
| 75 | +), |
| 76 | +combine_app_store_data AS ( |
| 77 | + SELECT |
28 | 78 | `date`, |
29 | | - country_name |
| 79 | + country_name, |
| 80 | + impressions, |
| 81 | + total_downloads, |
| 82 | + first_time_downloads, |
| 83 | + redownloads, |
| 84 | + FROM |
| 85 | + historical_store_data |
| 86 | + WHERE |
| 87 | + `date` < "2024-01-01" |
| 88 | + UNION ALL |
| 89 | + SELECT |
| 90 | + `date`, |
| 91 | + country_name, |
| 92 | + impressions, |
| 93 | + total_downloads, |
| 94 | + first_time_downloads, |
| 95 | + redownloads, |
| 96 | + FROM |
| 97 | + app_store_data |
| 98 | + WHERE |
| 99 | + `date` >= "2024-01-01" |
30 | 100 | ), |
31 | | -store_stats AS ( |
| 101 | +normalize_country AS ( |
32 | 102 | SELECT |
33 | | - DATE(`date`) AS `date`, |
| 103 | + `date`, |
34 | 104 | country_names.code AS country, |
35 | | - views, |
| 105 | + impressions, |
36 | 106 | total_downloads, |
37 | 107 | first_time_downloads, |
38 | 108 | redownloads, |
39 | 109 | FROM |
40 | | - views_data |
41 | | - FULL OUTER JOIN |
42 | | - downloads_data |
43 | | - USING (`date`, country_name) |
| 110 | + combine_app_store_data |
44 | 111 | LEFT JOIN |
45 | 112 | `moz-fx-data-shared-prod.static.country_names_v1` AS country_names |
46 | | - ON country_names.name = views_data.country_name |
| 113 | + ON combine_app_store_data.country_name = country_names.name |
47 | 114 | ), |
48 | 115 | _new_profiles AS ( |
49 | 116 | SELECT |
50 | 117 | first_seen_date AS `date`, |
51 | | - first_reported_country AS country, |
52 | | - COUNT(*) AS new_profiles, |
| 118 | + country, |
| 119 | + SUM(new_profiles) AS new_profiles, |
53 | 120 | FROM |
54 | | - `moz-fx-data-shared-prod.firefox_ios.firefox_ios_clients` |
| 121 | + `moz-fx-data-shared-prod.firefox_ios.new_profiles` |
55 | 122 | WHERE |
56 | 123 | first_seen_date = DATE_SUB(@submission_date, INTERVAL 7 DAY) |
57 | | - AND channel = "release" |
| 124 | + AND normalized_channel = "release" |
58 | 125 | GROUP BY |
59 | | - `date`, |
60 | | - country |
| 126 | + ALL |
61 | 127 | ) |
62 | 128 | SELECT |
63 | 129 | @submission_date AS submission_date, |
64 | 130 | `date` AS first_seen_date, |
65 | 131 | country, |
66 | | - COALESCE(views, 0) AS impressions, |
67 | | - COALESCE(total_downloads, 0) AS total_downloads, |
68 | | - COALESCE(first_time_downloads, 0) AS first_time_downloads, |
69 | | - COALESCE(redownloads, 0) AS redownloads, |
| 132 | + impressions, |
| 133 | + total_downloads, |
| 134 | + first_time_downloads, |
| 135 | + redownloads, |
70 | 136 | COALESCE(new_profiles, 0) AS new_profiles, |
71 | 137 | FROM |
72 | | - store_stats |
73 | | -FULL OUTER JOIN |
| 138 | + normalize_country |
| 139 | +LEFT JOIN |
74 | 140 | _new_profiles |
75 | 141 | USING (`date`, country) |
0 commit comments