festinko

Untitled

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