Skip to content

Commit

Permalink
✨ More detailed campaign stats
Browse files Browse the repository at this point in the history
  • Loading branch information
KONFeature committed Nov 8, 2024
1 parent 0067aa8 commit a2dc745
Showing 1 changed file with 89 additions and 3 deletions.
92 changes: 89 additions & 3 deletions packages/ponder/src/api/admin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ponder } from "@/generated";
import { eq } from "@ponder/core";
import { countDistinct, eq, inArray } from "@ponder/core";
import { type Address, isAddress } from "viem";

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand Down Expand Up @@ -78,6 +78,90 @@ ponder.get("/admin/:wallet/campaigns", async (ctx) => {
});

// Get all the campaign stats for a wallet
ponder.get("/admin/:wallet/campaignsStats", async (ctx) => {
// Extract wallet
const wallet = ctx.req.param("wallet") as Address;
if (!isAddress(wallet)) {
return ctx.text("Invalid wallet address", 400);
}

// Get the tables we will query
const {
ProductAdministrator,
Campaign,
ReferralCampaignStats,
InteractionEvent,
Product,
ProductInteractionContract,
} = ctx.tables;

// Perform the sql query
const campaignsStats = await ctx.db
.select({
productId: ProductAdministrator.productId,
isOwner: ProductAdministrator.isOwner,
roles: ProductAdministrator.roles,
id: Campaign.id,
bank: Campaign.bankingContractId,
totalInteractions: ReferralCampaignStats.totalInteractions,
openInteractions: ReferralCampaignStats.openInteractions,
readInteractions: ReferralCampaignStats.readInteractions,
referredInteractions: ReferralCampaignStats.referredInteractions,
createReferredLinkInteractions:
ReferralCampaignStats.createReferredLinkInteractions,
purchaseStartedInteractions:
ReferralCampaignStats.purchaseStartedInteractions,
purchaseCompletedInteractions:
ReferralCampaignStats.purchaseCompletedInteractions,
totalRewards: ReferralCampaignStats.totalRewards,
})
.from(ProductAdministrator)
.innerJoin(
Campaign,
eq(ProductAdministrator.productId, Campaign.productId)
)
.innerJoin(
ReferralCampaignStats,
eq(Campaign.id, ReferralCampaignStats.campaignId)
)
.where(eq(ProductAdministrator.user, wallet));

// Get the unique wallet on this product
if (campaignsStats.length === 0) {
return ctx.json({ stats: [] });
}

// Get all the product ids for this admin
const campaignProductIds = campaignsStats.map((c) => c.productId);
const uniqueProductIds = [...new Set(campaignProductIds)];

// Get the total number of unique users per product
const totalPerProducts = await ctx.db
.select({
productId: Product.id,
wallets: countDistinct(InteractionEvent.user),
})
.from(InteractionEvent)
.innerJoin(
ProductInteractionContract,
eq(InteractionEvent.interactionId, ProductInteractionContract.id)
)
.innerJoin(
Product,
eq(ProductInteractionContract.productId, Product.id)
)
.where(inArray(Product.id, uniqueProductIds))
.groupBy(Product.id);

// Return the result as json
return ctx.json({
stats: campaignsStats,
users: totalPerProducts,
});
});

// Get all the campaign stats for a wallet
// todo: For legacy purpose only, to be removed once prod is updated
ponder.get("/admin/:wallet/campaigns/stats", async (ctx) => {
// Extract wallet
const wallet = ctx.req.param("wallet") as Address;
Expand All @@ -103,8 +187,10 @@ ponder.get("/admin/:wallet/campaigns/stats", async (ctx) => {
referredInteractions: ReferralCampaignStats.referredInteractions,
createReferredLinkInteractions:
ReferralCampaignStats.createReferredLinkInteractions,
purchaseStartedInteractions: ReferralCampaignStats.purchaseStartedInteractions,
purchaseCompletedInteractions: ReferralCampaignStats.purchaseCompletedInteractions,
purchaseStartedInteractions:
ReferralCampaignStats.purchaseStartedInteractions,
purchaseCompletedInteractions:
ReferralCampaignStats.purchaseCompletedInteractions,
totalRewards: ReferralCampaignStats.totalRewards,
})
.from(ProductAdministrator)
Expand Down

0 comments on commit a2dc745

Please sign in to comment.