Updated views in a few key ways

This commit is contained in:
2025-11-12 14:58:09 +00:00
parent 45bc817428
commit 56e1edb0db
2 changed files with 91 additions and 8 deletions

View File

@@ -2,15 +2,20 @@
# This file stores important metadata that links different accounts and identifiers
accounts:
- label: "Example Hotel 1"
meta_account_id: "act_123456789"
google_account_id: "987-654-3210"
alpinebits_hotel_code: "HOTEL001"
- label: "Hotel Bemelmans Post"
meta_account_id: "238334370765317"
google_account_id: "7581209925"
alpinebits_hotel_code: "39054_001"
- label: "Jagdhof-Kaltern"
#meta_account_id: "act_987654321"
google_account_id: "1951919786"
alpinebits_hotel_code: "39052_001"
- label: "Residence Erika"
google_account_id: "6604634947"
alpinebits_hotel_code: "39040_001"
- label: "Example Hotel 2"
meta_account_id: "act_987654321"
google_account_id: "123-456-7890"
alpinebits_hotel_code: "HOTEL002"
# Add more accounts as needed
# - label: "Your Account Name"

View File

@@ -8,6 +8,9 @@ DROP SCHEMA IF EXISTS public CASCADE;
CREATE SCHEMA public;
-- Set ownership to meta_user
ALTER SCHEMA public OWNER TO meta_user;
-- ============================================================================
-- METADATA TABLE
-- ============================================================================
@@ -56,6 +59,9 @@ CREATE VIEW campaign_insights AS
ctr,
cpc,
cpm,
cpp,
frequency,
objective,
( SELECT (jsonb_array_elements.value ->> 'value'::text)::numeric AS "numeric"
FROM jsonb_array_elements(customcampaign_insights.actions) jsonb_array_elements(value)
WHERE (jsonb_array_elements.value ->> 'action_type'::text) = 'link_click'::text) AS link_click,
@@ -206,8 +212,78 @@ CREATE VIEW campaign_insights_by_country_flattened AS
FROM meta.custom_campaign_country;
-- ============================================================================
-- account views
DROP VIEW IF EXISTS account_insights_by_gender CASCADE;
CREATE VIEW account_insights_by_gender AS
SELECT
time,
account_id,
gender,
SUM(impressions) AS impressions,
SUM(clicks) AS clicks,
SUM(spend) AS spend,
SUM(link_click) AS link_click,
SUM(landing_page_view) AS landing_page_view,
SUM(lead) AS lead
FROM campaign_insights_by_gender
GROUP BY time, account_id, gender;
DROP VIEW IF EXISTS account_insights_by_age CASCADE;
CREATE VIEW account_insights_by_age AS
SELECT
time,
account_id,
age,
SUM(impressions) AS impressions,
SUM(clicks) AS clicks,
SUM(spend) AS spend,
SUM(link_click) AS link_click,
SUM(landing_page_view) AS landing_page_view,
SUM(lead) AS lead
FROM campaign_insights_by_age
GROUP BY time, account_id, age;
DROP VIEW IF EXISTS account_insights_by_gender_and_age CASCADE;
CREATE VIEW account_insights_by_gender_and_age AS
SELECT
time,
account_id,
gender,
age,
SUM(impressions) AS impressions,
SUM(clicks) AS clicks,
SUM(spend) AS spend,
SUM(link_click) AS link_click,
SUM(landing_page_view) AS landing_page_view,
SUM(lead) AS lead
FROM campaign_insights_by_gender_and_age
GROUP BY time, account_id, age, gender;
CREATE VIEW account_insights AS
SELECT
time,
account_id,
SUM(impressions) AS impressions,
SUM(clicks) AS clicks,
SUM(spend) AS spend,
SUM(link_click) AS link_click,
SUM(landing_page_view) AS landing_page_view,
SUM(lead) AS lead,
AVG(frequency) as frequency,
avg(cpc) as cpc,
avg(cpm) as cpm,
avg(cpp) as cpp,
avg(ctr) as ctr
FROM campaign_insights
group by time, account_id;
@@ -215,6 +291,8 @@ CREATE VIEW campaign_insights_by_country_flattened AS
-- Permission grants for Grafana user
GRANT USAGE ON SCHEMA public TO grafana;
-- Grant SELECT on all existing tables and views in the schema
GRANT SELECT ON ALL TABLES IN SCHEMA public TO grafana;