Skip to content

Commit

Permalink
config: Analytics mixpanel
Browse files Browse the repository at this point in the history
  • Loading branch information
abhijeetnishal committed May 19, 2024
1 parent 52854f7 commit 375a63d
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 6 deletions.
5 changes: 4 additions & 1 deletion server/.env.example
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
NODE_ENV=development
CLIENT_PROD_URL=http://localhost:3000
DB_URL=
REDIRECT_URL=http://localhost:8080

DB_URL=

JWT_SECRET_KEY=

REDIS_URL=
HITS_COUNT_LIMIT=100
EXPIRATION_TIME=60
File renamed without changes.
18 changes: 18 additions & 0 deletions server/config/mixpanel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const Mixpanel = require("mixpanel");

// track an event with optional properties
const trackEvent = (eventName, value) => {
const mixPanelProjectToken = process.env.MIXPANEL_PROJECT_TOKEN;

if (mixPanelProjectToken) {
const mixpanel = Mixpanel.init(process.env.MIXPANEL_PROJECT_TOKEN, {
geolocate: true,
});

mixpanel.track(eventName, {
url: value,
});
}
};

module.exports = trackEvent;
2 changes: 1 addition & 1 deletion server/controller/auth.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const userModel = require("../model/userSchema");
const jwt = require("jsonwebtoken");
const bcrypt = require("bcrypt");
const dbConnect = require("../model/dbConnect");
const dbConnect = require("../config/dbConnect");

const signup = async (req, res) => {
try {
Expand Down
13 changes: 11 additions & 2 deletions server/controller/urls.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const urlModel = require("../model/urlSchema");
const validUrl = require("valid-url");
const uniqueString = require("../utils/utils");
const dbConnect = require("../model/dbConnect");
const dbConnect = require("../config/dbConnect");
const dotenv = require("dotenv");
const trackEvent = require("../config/mixpanel");

dotenv.config();

Expand Down Expand Up @@ -52,7 +53,12 @@ const createUrl = async (req, res) => {
// If the URL exists, return the existing shortId
if (urlExist) {
const shortId = urlExist.shortId;
return res.status(201).json(`${process.env.REDIRECT_URL}/${shortId}`);
const shortenedURL = `${process.env.REDIRECT_URL}/${shortId}`;

// track event
trackEvent("Existing URL", shortenedURL);

return res.status(201).json(shortenedURL);
} else {
// If the URL does not exist, generate a new shortId and save the URL to the database
const shortId = uniqueString.generateBase62String();
Expand All @@ -65,6 +71,9 @@ const createUrl = async (req, res) => {
// Save the new URL to the database
await newUrl.save();

// track event
trackEvent("New URL shortened", originalUrl);

// Send the newly created short URL to the client
return res.status(201).json(`${process.env.REDIRECT_URL}/${shortId}`);
}
Expand Down
2 changes: 1 addition & 1 deletion server/controller/user.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const urlModel = require("../model/urlSchema");
const dbConnect = require("../model/dbConnect");
const dbConnect = require("../config/dbConnect");
const dotenv = require("dotenv");

dotenv.config();
Expand Down
2 changes: 1 addition & 1 deletion server/model/urlSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const urlSchema = new mongoose.Schema(
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
required: true,
required: false,
},
originalUrl: {
type: String,
Expand Down
77 changes: 77 additions & 0 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"express": "^4.18.2",
"ioredis": "^5.4.1",
"jsonwebtoken": "^9.0.2",
"mixpanel": "^0.18.0",
"mongoose": "^7.0.5",
"valid-url": "^1.0.9"
},
Expand Down

0 comments on commit 375a63d

Please sign in to comment.