BigQuery showing 0 results but Looker showing 2 - google-bigquery

I'm trying to get data for all campaigns that have a traffic_source.source of Linkedin and traffic_source.medium of paid for one day, and here's my code:
SELECT traffic_source.medium, traffic_source.name, traffic_source.source, COUNT(*)
FROM `nth-glider-369017.analytics_316822874.events_20230206`
WHERE event_name = 'sr_job_completed_application'
AND traffic_source.medium = 'paid'
GROUP BY traffic_source.medium, traffic_source.name, traffic_source.source
I get 0 results, however when i do the same thing in Looker Studio, I see that I have 2 events
Paid LinkedIn event
Why would BQ be showing different data than Looker for that?

Related

BigQuery Google Data Transfer Impressions & Cost Don't Match Google Ads UI

Disclaimer...I'm a Noob
I am writing a query from CampaignStats table that aggregates based on a stripped campaign label. The query returns correct values for all metrics except Impressions and Cost. No matter what I've tried so far, this figure still doesn't match. Here are the totals for two of my campaigns from yesterday (June 17th):
CampaignStats:
Date label Impressions cost clicks avg_cpc
6/17/2022 sat_brand 2687 140.472666 15 9.3648444
CampaignBasicStats:
Date label Impressions cost clicks avg_cpc
6/17/2022 sat_brand 699 152.620961 15 10.17473073
Utilizing the CampaignBasicStats table, I receive aggregated totals for all metrics that match the UI, including Impressions and Cost. The issue is there are metrics in CampaignStats and getting some illumination/information on what I may not be doing correct, will help in the future.
I did a JOIN with the Campaign table originally; the below query refers to a permanent table that I pulled out separately in case this was a cause of the discrepancy.
Code Below:
SELECT
cs.Date,
EXTRACT(ISOWEEK FROM cs.DATE) AS isoweek,
cl.label,
(SUM(cs.Cost) / 1000000) AS cost,
SUM(cs.Clicks) AS clicks,
CASE WHEN SUM(Clicks)=0 OR SUM(Cost)=0 THEN 0 ELSE
((SUM(Cost)/SUM(Clicks))/1000000) END AS avg_cpc,
SUM(cs.Impressions) AS Impressions,
CASE WHEN SUM(cs.Clicks)=0 THEN 0 ELSE
(SUM(cs.Clicks)/SUM(cs.Impressions)) END AS ctr,
SUM(cs.Conversions) as conversions,
CASE WHEN SUM(cs.Conversions)=0 OR SUM(cs.Clicks)=0 THEN 0 ELSE
(SUM(cs.Conversions)/SUM(cs.Clicks)) END AS cvr,
CASE WHEN SUM(cs.Conversions)=0 OR SUM(cs.Cost)=0 THEN 0 ELSE
(SUM(cs.Cost)/SUM(cs.Conversions))/1000000 END AS cost_per_conversion
FROM
`bold-quanta-######.######_google_ads_dataset.CampaignStats_##########` cs
JOIN
`bold-quanta-######.queried_permanent_tables.process_campaign_labels` cl
ON
cs.CampaignId = cl.CampaignId
GROUP BY
1, 2, 3

Firebase export to BigQuery not populating traffic_source.name (campaign name) starting Feb 10th

We noticed an issue across all our projects starting Feb 10th where the first_open event contains an empty campaign name (traffic_source.name column). The traffic_source.source and traffic_source.medium columns contain the right values indicating the users are from Google Ads campaigns (google and cpc respectively).
Following is the BigQuery select (just replace the {project} and {ID}):
SELECT event_date, traffic_source.source, traffic_source.medium, traffic_source.name, count(*)
FROM `{PROJECT}.analytics_{ID}.events_202202*`
WHERE event_name = 'first_open' AND traffic_source.medium = 'cpc' and traffic_source.name IS NULL
group by 1,2,3,4
order by 1,2,3,4
I've checked the first_open attribution on Firebase console and it seems to be fine. We suspect the issue is related to the export to BigQuery only.
Does anyone else notice this?

Computing conversion rate by counting TRUE/FALSE statements

Sorry if this sounds very basic; bear with me.
I need to determine the conversion rate of 3 ads, each representing a product; that would be subscription divided by the number of people who clicked the ad.
COLUMNS:
person_id - unique identifier of the person
date - date they were shown the ad
ad_id - content of the ad: ad_1_product1, ad_2_product2, or ad_3_product3
clicked (TRUE/FALSE) - clicked on the ad
signed_up - (TRUE/FALSE) created an account
subscribed (TRUE/FALSE) - started a paid subscription
I set clicked, signed_up and subscribed as boolean.
MY CODE:
SELECT ad_id, (count(subscribed) / count(clicked)) as CR
FROM videoadcampaign
WHERE subscribed = 'TRUE' AND clicked = 'TRUE'
GROUP BY ad_id;
Of course, the code above gives me a ratio of 1, because SQL is still counting the total and dividing by the same number because of those conditions.
I am totally stuck.
I will also need to calculate other KPIs for clicks and signed_up, so filtering those booleans and put them into a ratio is the core of what I need to do.
Is there a way I can tell SQL to compute CR = SUBS (TRUE) / SUBS (TRUE + FALSE) [or total count] and then filter by CLICK = TRUE?
Thank you tons for your help!
It depends on your database, but the general notion would be:
SELECT ad_id,
(SUM(CASE WHEN subscribed = 'TRUE' THEN 1.0 ELSE 0 END) /
SUM(CASE WHEN clicked = 'TRUE' THEN 1 ELSE 0 END)
) as CR
FROM videoadcampaign
GROUP BY ad_id;
In many databases, you can do something like this if the columns are integers (0 = false, 1 = true):
SELECT ad_id, SUM(subscribed) / SUM(clicked) as CR
FROM videoadcampaign
WHERE clicked = 'TRUE'
GROUP BY ad_id;
Or even:
SELECT ad_id, AVG(subscribed) as CR
FROM videoadcampaign
WHERE clicked = 'TRUE'
GROUP BY ad_id;

How to get "session duration" group by "operating system" in Firebase Bigquery SQL?

I try to get the "average session duration" group by "operating system" (device.operating_system) and "date" (event_date).
In the firebase blog, they give us this query to get the average duration session
SELECT SUM(engagement_time) AS total_user_engagement
FROM (
SELECT user_pseudo_id,
(SELECT value.int_value FROM UNNEST(event_params) WHERE key =
"engagement_time_msec") AS engagement_time
FROM `FIREBASE_PROJECT`
)
WHERE engagement_time > 0
GROUP BY user_pseudo_id
This query give me the total user engagement by user ID (each row is a different user):
row|total_user_engagement
---|------------------
1 |989646
2 |225655
3 |125489
4 | 58496
...|......
But I have no idea where I have to add the "operating system" and "event_date" variables to get this information by os and date. I tried differents queries with no result. For example to get this result by operatiing system I tried the following
SELECT SUM(engagement_time) AS total_user_engagement
FROM (
SELECT device.operating_system,
(SELECT value.int_value FROM UNNEST(event_params) WHERE key =
"engagement_time_msec") AS engagement_time
FROM `FIREBASE_PROJECT`
)
WHERE engagement_time > 0
GROUP BY device.operating_system
But it gives me an error message (Error: Unrecognized name: device at [9:10] ). In others queries device.operating_system is recognized.
For example in that one :
SELECT
event_date,
device.operating_system as os_type,
device.operating_system_version as os_version,
device.mobile_brand_name as device_brand,
device.mobile_model_name as device_model,
count(distinct user_pseudo_id) as all_users
FROM `FIREBASE Project`
GROUP BY 1,2,3,4,5
What I would like to have as a result is something like this :
row|event_date|OS |total_user_engagement
---|----------------------------------------
1 |20191212 |ios |989646
2 |20191212 |android|225655
3 |20191212 |ios |125489
4 |20191212 |android| 58496
...
Thank you
The error is probably because you are referencing the variable device in the outer query, while this variable is only visible from the inner query (subquery). I believe the issue will be fixed by changing the last row of the query from GROUP BY device.operating_system
to
GROUP BY operating_system.
Hopefully this will make clearer what is happening here: the inner query is accessing the table FIREBASE_PROJECT and returning the field operating_system from the nested column device. The outer query accesses the results of the inner query, so it only sees the returned field operating_system, without information about its original context within the nested variable device. That is why trying to reference device at this level will fail.
In the other example you posted this issue does not appear, since there is only a simple query.

Sum in Decode statement?

I have to provide counts for different activities, some of the columns are from some table but few I have to take by joining other tables. And in the last column I have to add counts *of 5 columns togather in one field*.Please see my query below and advice the best way to achieve my results :)
SELECT web. OID,web. MARKETING_GROUP,
SUM(DECODE(WEB.EVENT_TYPE,5,WEB.ACTIVITY_COUNT,0)) AS DISCUSSIONCOMMENT ,
SUM(DECODE(WEB.EVENT_TYPE,6,WEB.ACTIVITY_COUNT,0)) AS DISCUSSIONSTART ,
SUM(DECODE(WEB.EVENT_TYPE,7,WEB.ACTIVITY_COUNT,0)) AS DISCUSSIONVIEW,
SUM(case when o.when _clicked is not null then c(*))AS clickcount,
**SUM(case when WEB.EVENT_TYPE in(5,6,7,8)then WEB.ACTIVITY_COUNT +c.count(*))as Total** --------- This is where I am getting stuck and confused???
from GMMI_AIR.WEB_ACTIVITY_FCT WEB join GMMI_AIR.COUPON_WEB_ACTIVITY C
on WEB.OID_WEB_ACTIVITY_FCT = C.OID_COUPON_WEB_ACTIVITY
I am really stuck as the table which is joined doesn'thave field named activity_count or activity_type so to see the counts I can only do count(*) but to add it all other fileds in one column and sum it up is very confusing??
Any help??/
SELECT web. OID,web. MARKETING_GROUP,
SUM(DECODE(WEB.EVENT_TYPE,5,WEB.ACTIVITY_COUNT,0)) AS DISCUSSIONCOMMENT ,
SUM(DECODE(WEB.EVENT_TYPE,6,WEB.ACTIVITY_COUNT,0)) AS DISCUSSIONSTART ,
SUM(DECODE(WEB.EVENT_TYPE,7,WEB.ACTIVITY_COUNT,0)) AS DISCUSSIONVIEW,
SUM(case when o.when _clicked is not null then c(*))AS clickcount,
SUM(case when WEB.EVENT_TYPE in(5,6,7,8)then WEB.ACTIVITY_COUNT END) +c.count(*) as Total
from GMMI_AIR.WEB_ACTIVITY_FCT WEB join GMMI_AIR.COUPON_WEB_ACTIVITY C
on WEB.OID_WEB_ACTIVITY_FCT = C.OID_COUPON_WEB_ACTIVITY
GROUP BY web. OID,web. MARKETING_GROUP;