-
Notifications
You must be signed in to change notification settings - Fork 12
/
linkedin_ads__url_report.sql
98 lines (79 loc) · 2.89 KB
/
linkedin_ads__url_report.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
{{ config(enabled=var('ad_reporting__linkedin_ads_enabled', True)) }}
with creative as (
select *
from {{ var('creative_history') }}
where is_latest_version
),
campaign as (
select *
from {{ var('campaign_history') }}
where is_latest_version
),
campaign_group as (
select *
from {{ var('campaign_group_history') }}
where is_latest_version
),
account as (
select *
from {{ var('account_history') }}
where is_latest_version
),
report as (
select *,
{% if var('linkedin_ads__conversion_fields', none) %}
{{ var('linkedin_ads__conversion_fields') | join(' + ') }} as total_conversions
{% else %}
0 as total_conversions
{% endif %}
from {{ var('ad_analytics_by_creative') }}
),
final as (
select
report.source_relation,
report.date_day,
creative.click_uri,
creative.click_uri_type,
creative.base_url,
creative.url_host,
creative.url_path,
creative.utm_source,
creative.utm_medium,
creative.utm_campaign,
creative.utm_content,
creative.utm_term,
report.creative_id,
campaign.campaign_id,
campaign.campaign_name,
campaign_group.campaign_group_id,
campaign_group.campaign_group_name,
account.account_id,
account.account_name,
account.currency,
sum(report.total_conversions) as total_conversions,
sum(report.clicks) as clicks,
sum(report.impressions) as impressions,
sum(report.cost) as cost,
sum(coalesce(report.conversion_value_in_local_currency, 0)) as conversion_value_in_local_currency
{{ linkedin_ads_persist_pass_through_columns(pass_through_variable='linkedin_ads__conversion_fields', transform='sum', coalesce_with=0, except_variable='linkedin_ads__creative_passthrough_metrics', exclude_fields=['conversion_value_in_local_currency']) }}
{{ linkedin_ads_persist_pass_through_columns(pass_through_variable='linkedin_ads__creative_passthrough_metrics', transform='sum', exclude_fields=['conversion_value_in_local_currency']) }}
from report
left join creative
on report.creative_id = creative.creative_id
and report.source_relation = creative.source_relation
left join campaign
on creative.campaign_id = campaign.campaign_id
and creative.source_relation = campaign.source_relation
left join campaign_group
on campaign.campaign_group_id = campaign_group.campaign_group_id
and campaign.source_relation = campaign_group.source_relation
left join account
on campaign.account_id = account.account_id
and campaign.source_relation = account.source_relation
{% if var('ad_reporting__url_report__using_null_filter', True) %}
where creative.click_uri is not null
{% endif %}
{{ dbt_utils.group_by(20) }}
)
select *
from final