festinko

Untitled

Aug 11th, 2022
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 9.86 KB | None | 0 0
  1. WITH
  2.  
  3.  ---added user_id compared to the query from
  4.         flat_user_properties AS (
  5.         select
  6.             user_pseudo_id,
  7.             event_timestamp,
  8.             event_name,
  9.             user_id,
  10.             user_properties.key,
  11.             user_properties.value.string_value,
  12.             user_properties.value.int_value,
  13.             user_properties.value.float_value,
  14.             user_properties.value.double_value,
  15.             user_properties.value.set_timestamp_micros,
  16.             concat(user_pseudo_id,event_timestamp,event_name,dedup_id) as join_key
  17.         from (
  18.             select
  19.                 row_number() over(partition by user_pseudo_id, event_timestamp, event_name) as dedup_id,
  20.                 *
  21.             from
  22.                 -- change this to your google analytics 4 export location in bigquery
  23.                 `uptimerobot-001.analytics_220800152.events_*`
  24.             where
  25.             -- define static and/or dynamic start and end date
  26.             _table_suffix between '20220701' and '20220809'),
  27.             unnest(user_properties) as user_properties
  28.             ),
  29.  
  30.  
  31. /*First payment of all customers in Lifetime*/
  32. CustomerFirstPayment as (
  33.   SELECT
  34.     p.userID,
  35.     MAX(paymentDateTime) as paymentDateTime,
  36.     MAX(firstPaymentDateTime) as firstPaymentDateTime,
  37.     paymentPeriod,
  38.     paymentProcessor,
  39.     paymentCurrency,
  40.     MAX(paymentVATRatio) as paymentVATRatio,
  41.     MAX(productID) as productID,
  42.     MAX(
  43.     (CASE
  44.       WHEN paymentCurrency = "EUR"  THEN paymentAmount * 1.07
  45.       ELSE paymentAmount
  46.     END)) as paymentAmountUSD,
  47.     MAX (paymentAmount) as realPaymentAmountPaid,
  48.     ROUND(SAFE_DIVIDE(paymentAmount,paymentPeriod),0) as MRR
  49.  
  50.   FROM
  51.     `uptimerobot-001.ur.payments` p
  52.     LEFT JOIN
  53.         (SELECT
  54.           userID,
  55.           MIN(paymentDateTime) as firstPaymentDateTime
  56.         FROM
  57.         `uptimerobot-001.ur.payments`
  58.         WHERE
  59.           paymentType = 2 AND
  60.           paymentStatus = 1
  61.  
  62.         GROUP BY
  63.           userID
  64.         ) fp
  65.     ON
  66.       p.userID = fp.userID and p.paymentDateTime = fp.firstPaymentDateTime
  67.  
  68.     WHERE
  69.       firstPaymentDateTime IS NOT NULL
  70.       AND          
  71.       -- define static and/or dynamic start and end date - align with GA4 data reporting date range
  72.           paymentDateTime > '2022-07-01 00:00:01' AND paymentDateTime < '2022-08-09 23:59:59'
  73.  
  74.     GROUP BY
  75.       userID,
  76.       paymentPeriod,
  77.       paymentProcessor,
  78.       paymentCurrency,
  79.       MRR
  80.  
  81.     ORDER BY
  82.         MRR DESC),
  83.  
  84. /*A/B test dimension value by userID*/
  85.   ABtestAsigned as (
  86.    SELECT
  87.     flat_user_properties.user_id,
  88.     key,
  89.     string_value as ABTestVariant
  90.  
  91.   FROM
  92.     flat_user_properties
  93.   WHERE
  94.      key = "ABTest"
  95.  
  96.   GROUP BY
  97.     user_id,
  98.     key,
  99.     ABTestVariant
  100.   ),
  101.  
  102. /*userID's with multiple values in A/B test dimension*/
  103.  multipleABTestVariant as (SELECT
  104.    user_id,
  105.    count(user_id) userIDcount
  106.  
  107.   FROM
  108.     ABtestAsigned
  109.   GROUP BY (user_id)
  110.   HAVING userIDcount > 1),
  111.  
  112. /*A/B test dimension value by userID w/o userID's with multiple values in A/B test dimension*/
  113.   withoutMultipleABTestVariant as (
  114.     SELECT
  115.       *
  116.     FROM
  117.       ABtestAsigned
  118.     WHERE
  119.       user_ID NOT IN (SELECT user_ID FROM multipleABTestVariant)
  120.     ORDER BY user_id),
  121.  
  122. /*G4 by userID*/
  123.   G4 as (
  124.     SELECT
  125.       user_id,
  126.       event_name
  127.  
  128.     FROM
  129.       flat_user_properties
  130.     WHERE
  131.       event_name LIKE ("G4_First_Subscription_Paid")
  132.     GROUP BY
  133.       user_id,
  134.       event_name
  135.   ),
  136.  
  137. /*UserID with A/B test dimension Connected with users who completed G4*/
  138.  
  139.   ABtest as (
  140.     SELECT
  141.       withoutMultipleABTestVariant.user_id as user_id,
  142.       key,
  143.       ABTestVariant,
  144.       (CASE
  145.         WHEN event_name IS NULL THEN "NotConvertedInGA4"
  146.         WHEN event_name LIKE ("G4_First_Subscription_Paid") THEN "G4_First_Subscription_Paid"
  147.         ELSE "WTF"
  148.       END) as convertedinGA4
  149.        
  150.  
  151.     FROM
  152.       withoutMultipleABTestVariant
  153.     LEFT JOIN
  154.       G4
  155.     ON withoutMultipleABTestVariant.user_id = G4.user_id
  156.  
  157.   ),
  158.  
  159. /*REDUNDANT list of users from GA4 completed G4 with ABTestVariant dimenssion
  160.   ABTest2 as
  161.     (SELECT
  162.           user_id,
  163.           event_name,
  164.           key,
  165.           string_value as ABTestVariant,
  166.           event_timestamp
  167.  
  168.       FROM
  169.           flat_user_properties
  170.       WHERE
  171.           key = "ABTest"
  172.           AND event_name LIKE ("G4_First_Subscription_Paid")
  173.     ),*/
  174.  
  175.  
  176.   preparedData as (SELECT
  177.       fp.userID,
  178.       paymentDateTime,
  179.       firstPaymentDateTime,
  180.       paymentPeriod,
  181.       paymentProcessor,
  182.       productID,
  183.       realPaymentAmountPaid,
  184.       realPaymentAmountPaid/(1+(paymentVATRatio/100)) as realPaymentAmountPaidVATexcluded,
  185.       paymentAmountUSD,
  186.       paymentCurrency,
  187.       ABTestVariant,
  188.       /*REDUNDANT
  189.       (CASE
  190.         WHEN ABTestVariant = "early-bird-ab / EarlyBird" AND paymentPeriod = 12 THEN paymentAmountUSD / 0.7 / 12
  191.         WHEN ABTestVariant = "early-bird-ab / EarlyBird" AND paymentPeriod = 1 THEN paymentAmountUSD / 0.5
  192.         WHEN ABTestVariant = "early-bird-ab / EarlyBirdLowerDiscou" AND paymentPeriod = 12 THEN paymentAmountUSD / 0.9 / 12
  193.         WHEN ABTestVariant = "early-bird-ab / EarlyBirdLowerDiscou" AND paymentPeriod = 1 THEN paymentAmountUSD / 0.9
  194.         WHEN ABTestVariant = "early-bird-ab / NoEarlyBird" AND paymentPeriod = 12 THEN paymentAmountUSD / 12
  195.         WHEN ABTestVariant = "early-bird-ab / NoEarlyBird" AND paymentPeriod = 1 THEN paymentAmountUSD
  196.       END
  197.       ) futureMRR,
  198.     (CASE
  199.         WHEN ABTestVariant = "early-bird-ab / EarlyBird" AND paymentPeriod = 12 THEN paymentAmountUSD / 0.7 * 0.3
  200.         WHEN ABTestVariant = "early-bird-ab / EarlyBird" AND paymentPeriod = 1 THEN paymentAmountUSD / 0.5 * 0.5
  201.         WHEN ABTestVariant = "early-bird-ab / EarlyBirdLowerDiscou" AND paymentPeriod = 12 THEN paymentAmountUSD / 0.9 * 0.1
  202.         WHEN ABTestVariant = "early-bird-ab / EarlyBirdLowerDiscou" AND paymentPeriod = 1 THEN paymentAmountUSD / 0.9 * 0.1
  203.         WHEN ABTestVariant = "early-bird-ab / NoEarlyBird" THEN 0
  204.       END
  205.       ) discountCost,*/
  206.       convertedinGA4
  207.  
  208.     FROM
  209.       CustomerFirstPayment fp
  210.  
  211.     LEFT JOIN
  212.       ABTest abt
  213.  
  214.     ON TO_HEX(MD5(CONCAT(fp.userID,'honvo'))) = abt.user_id
  215.  
  216.    WHERE
  217.       ABTestVariant IS NOT NULL),
  218.  
  219.   usersData as (
  220.     SELECT
  221.       pp.userID,
  222.       paymentDateTime,
  223.       firstPaymentDateTime,
  224.       userRegisterDateTime,
  225.       DATETIME_DIFF (CAST(firstPaymentDateTime AS DATETIME), CAST(userRegisterDateTime AS DATETIME), MINUTE)/60 as hoursToPayment,
  226.       (CASE
  227.         WHEN DATETIME_DIFF (CAST(firstPaymentDateTime AS DATETIME), CAST(userRegisterDateTime AS DATETIME), MINUTE)/60 > 1 THEN "more_than_hour"
  228.         ELSE "less_than_hour"
  229.       END
  230.       ) as hoursToPaymentBucket,
  231.       paymentPeriod,
  232.       userIsProPlan,
  233.       paymentProcessor,
  234.       productID,
  235.       realPaymentAmountPaid,
  236.       realPaymentAmountPaidVATexcluded,
  237.       paymentAmountUSD,
  238.       paymentCurrency,
  239.       ABTestVariant,
  240.       /*REDUNDANT
  241.       futureMRR,
  242.       discountCost,*/
  243.       convertedinGA4
  244.  
  245.     FROM
  246.       preparedData pp
  247.  
  248.     LEFT JOIN
  249.       `uptimerobot-001.ur.users` u
  250.     ON pp.userID = u.userID
  251.  
  252.   WHERE
  253.   userRegisterDateTime > '2022-07-01 00:00:01' AND paymentDateTime < '2022-08-09 23:59:59'
  254.  
  255.   ),
  256.  
  257. productData as (
  258.  
  259.   SELECT
  260.       userID,
  261.       paymentDateTime,
  262.       firstPaymentDateTime,
  263.       userRegisterDateTime,
  264.       hoursToPayment,
  265.       hoursToPaymentBucket,
  266.       paymentPeriod,
  267.       userIsProPlan,
  268.       paymentProcessor,
  269.       usersData.productID,
  270.       `uptimerobot-001.ur.products`.productID as productID2,
  271.       realPaymentAmountPaid,
  272.       realPaymentAmountPaidVATexcluded,
  273.       paymentAmountUSD,
  274.       paymentCurrency,
  275.       productValue,
  276.       ABTestVariant,
  277.       /* REDUNDANT
  278.       futureMRR,
  279.       discountCost,*/
  280.       productMonthlyPrice,
  281.       productAnnualPrice,
  282.       (CASE
  283.         WHEN paymentPeriod = 12 THEN productAnnualPrice
  284.         WHEN paymentPeriod = 1 THEN productMonthlyPrice
  285.         ELSE 99999999999999
  286.       END) correctProductPrice,
  287.       (CASE
  288.         WHEN paymentPeriod = 12 THEN productAnnualPrice / 12
  289.         WHEN paymentPeriod = 1 THEN productMonthlyPrice
  290.         ELSE 99999999999999
  291.       END) MRRProductPrice,
  292.       convertedinGA4
  293.  
  294.  
  295. FROM
  296.   usersData
  297.  
  298. LEFT JOIN
  299.   `uptimerobot-001.ur.products`
  300.  
  301. ON
  302.   usersData.productID = `uptimerobot-001.ur.products`.productID
  303.  
  304. ),
  305.  
  306. productData2 as (
  307.   SELECT
  308.       userID,
  309.       paymentDateTime,
  310.       firstPaymentDateTime,
  311.       userRegisterDateTime,
  312.       hoursToPayment,
  313.       hoursToPaymentBucket,
  314.       paymentPeriod,
  315.       userIsProPlan,
  316.       paymentProcessor,
  317.       productID,
  318.       realPaymentAmountPaid,
  319.       realPaymentAmountPaidVATexcluded,
  320.       paymentAmountUSD,
  321.       paymentCurrency,
  322.       productValue,
  323.       ABTestVariant,
  324.       ---futureMRR,
  325.       ---discountCost,
  326.       productMonthlyPrice,
  327.       productAnnualPrice,
  328.       MRRProductPrice,
  329.       (CASE
  330.         WHEN paymentCurrency IN ("EUR","eur")  THEN MRRProductPrice * 1.07
  331.         ELSE MRRProductPrice
  332.       END) as MRRProductPriceUSD,
  333.       ---futureMRR - MRRProductPrice as futureMRRProductMonthlyPriceDiff,
  334.       ROUND(realPaymentAmountPaidVATexcluded - correctProductPrice,2) as discountCost,
  335.       (CASE
  336.         WHEN ROUND(realPaymentAmountPaidVATexcluded - correctProductPrice,2) = 0 THEN "RealProductPrice"
  337.         WHEN ROUND(realPaymentAmountPaidVATexcluded - correctProductPrice,2) > 0 THEN "MoreThanRealProductPrice"
  338.         WHEN ROUND(realPaymentAmountPaidVATexcluded - correctProductPrice,2) < 0 THEN "LessThanRealProductPrice"
  339.         ELSE "WTF"
  340.         END
  341.       ) paidLessMoreRealThanProductPrice,
  342.       convertedinGA4
  343.  
  344. FROM
  345.   productData
  346. )
  347.  
  348.   SELECT
  349. *
  350.  
  351.   FROM
  352.     productData2
  353.  
  354.  
  355.  
  356.  
Add Comment
Please, Sign In to add comment