Skip to content

Commit

Permalink
Added analytics on transition
Browse files Browse the repository at this point in the history
  • Loading branch information
ishaan812 committed Jun 25, 2024
1 parent 382b034 commit 1a9f9df
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
22 changes: 15 additions & 7 deletions packages/nextjs/app/api/orchestrator/[frameId]/route.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import { NextRequest, NextResponse } from "next/server";
import { FrameMetadataType, FrameRequest, getFrameHtmlResponse, getFrameMessage } from "@coinbase/onchainkit";
import { FrameMetadataType, FrameRequest, getFrameHtmlResponse } from "@coinbase/onchainkit";
import Analytics from "~~/model/analytics";
import connectDB from "~~/services/connectDB";
import { getFrameById } from "~~/services/frames";

async function getResponse(req: NextRequest): Promise<NextResponse> {
await connectDB();
const body: FrameRequest = await req.json();
const { isValid } = await getFrameMessage(body);

if (!isValid) {
return new NextResponse("Message not valid", { status: 500 });
}

const state = JSON.parse(decodeURIComponent(body.untrustedData.state as string));
// Create Analytics for the frame asynchronously
const analyticsEntry = new Analytics({
journeyId: state?.journeyId || "",
frameId: state?.frameId || "",
fid: body.untrustedData.fid,
buttonClicked: body.untrustedData.buttonIndex || 0,
inputtedText: body.untrustedData.inputText || "",
timestamp: body.untrustedData.timestamp,
});
await analyticsEntry.save();
return new NextResponse(getFrameHtmlResponse(getFrameById(1) as FrameMetadataType));
}

Expand Down
29 changes: 29 additions & 0 deletions packages/nextjs/model/analytics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import mongoose, { Document, Schema } from "mongoose";

interface Analytics extends Document {
journeyId: string;
frameId: string;
fid: string;
buttonClicked: number;
inputtedText: string;
timestamp: Date;
}

const AnalyticsSchema: Schema<Analytics> = new Schema<Analytics>(
{
journeyId: String, // journeyId to correlate
frameId: String, //frameId to correlate
fid: String, // fid of the user
buttonClicked: Number, // 0 for no button clicked, 1 for button 1 clicked....
inputtedText: String, // text inputted by the user, null if empty
timestamp: Number, // timestamp of the event
},
{
timestamps: true,
},
);

// Define and export the Analytics model
const Analytics = mongoose.models.Analytics || mongoose.model<Analytics>("Analytics", AnalyticsSchema);

export default Analytics;
4 changes: 3 additions & 1 deletion packages/nextjs/services/connectDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ async function connectDB(): Promise<Mongoose> {
bufferCommands: false,
};

cached.promise = mongoose.connect(DATABASE_URL, opts).then(mongoose => mongoose);
cached.promise = mongoose.connect(DATABASE_URL, opts).then(mongoose => {
return mongoose;
});
}

cached.conn = await cached.promise;
Expand Down
4 changes: 4 additions & 0 deletions packages/nextjs/services/frames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export const getFrameById = (id: number) => {
return {
buttons: [
{
action: "post",
target: "http://localhost:3000/api/orchestrator/1",
label: `Home ${id}`,
},
{
Expand All @@ -19,6 +21,8 @@ export const getFrameById = (id: number) => {
},
state: {
time: new Date().toISOString(),
journey_id: "1",
frame_id: "2",
},
};
};

0 comments on commit 1a9f9df

Please sign in to comment.