minor improvements to UTM tracking accuracy

This commit is contained in:
Vladless
2025-06-14 07:01:42 +03:00
parent 40ccd8060a
commit cebdeb78fe
+26 -8
View File
@@ -74,15 +74,31 @@ async def get_all_tracking_sources(session: AsyncSession) -> list[dict]:
async def get_tracking_source_stats(session: AsyncSession, code: str) -> dict | None: async def get_tracking_source_stats(session: AsyncSession, code: str) -> dict | None:
source_result = await session.execute(
select(TrackingSource.created_at).where(TrackingSource.code == code)
)
created_at_row = source_result.first()
if not created_at_row:
return None
created_at = created_at_row[0]
reg_subq = ( reg_subq = (
select(func.count(func.distinct(User.tg_id))) select(func.count(func.distinct(User.tg_id)))
.where(User.source_code == code) .where(
(User.source_code == code) &
(User.created_at >= created_at)
)
.scalar_subquery() .scalar_subquery()
) )
trial_subq = ( trial_subq = (
select(func.count(func.distinct(User.tg_id))) select(func.count(func.distinct(User.tg_id)))
.where((User.source_code == code) & (User.trial == 1)) .where(
(User.source_code == code) &
(User.trial == 1) &
(User.created_at >= created_at)
)
.scalar_subquery() .scalar_subquery()
) )
@@ -90,9 +106,10 @@ async def get_tracking_source_stats(session: AsyncSession, code: str) -> dict |
select(func.count(func.distinct(Payment.tg_id))) select(func.count(func.distinct(Payment.tg_id)))
.join(User, Payment.tg_id == User.tg_id) .join(User, Payment.tg_id == User.tg_id)
.where( .where(
(User.source_code == code) (User.source_code == code) &
& (Payment.status == "success") (Payment.status == "success") &
& not_(Payment.payment_system.in_(["coupon", "referral"])) not_(Payment.payment_system.in_(["coupon", "referral", "cashback"])) &
(Payment.created_at >= created_at)
) )
.scalar_subquery() .scalar_subquery()
) )
@@ -101,9 +118,10 @@ async def get_tracking_source_stats(session: AsyncSession, code: str) -> dict |
select(func.coalesce(func.sum(Payment.amount), 0)) select(func.coalesce(func.sum(Payment.amount), 0))
.join(User, Payment.tg_id == User.tg_id) .join(User, Payment.tg_id == User.tg_id)
.where( .where(
(User.source_code == code) (User.source_code == code) &
& (Payment.status == "success") (Payment.status == "success") &
& not_(Payment.payment_system.in_(["coupon", "referral"])) not_(Payment.payment_system.in_(["coupon", "referral", "cashback"])) &
(Payment.created_at >= created_at)
) )
.scalar_subquery() .scalar_subquery()
) )