Skip to content

Commit

Permalink
Merge pull request #87 from Nazi-pikachu/frontend-posthog
Browse files Browse the repository at this point in the history
Feat : Added  frontend telemetry support
  • Loading branch information
Shruti3004 authored Sep 12, 2023
2 parents b24ee44 + deb1b96 commit b281c5d
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 9 deletions.
5 changes: 4 additions & 1 deletion apps/frontend/src/app/components/Header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { Space } from "antd";
import { useNavigate } from "react-router-dom";
import "app/styles/header.css";
import { Row, Col, Breadcrumb, Button } from "antd";

import Event from "app/constants/PosthogEvent";
import { NavLink, Link } from "react-router-dom";
import { usePostHog } from "posthog-js/react";

// TO DO: Shift these to constants file
const profile = [
Expand Down Expand Up @@ -39,12 +40,14 @@ const toggler = [
];

function Header({ name, subName, onPress }) {
const posthog = usePostHog();
const local_info = JSON.parse(localStorage.getItem("user-info"));
const navigate = useNavigate();

useEffect(() => window.scrollTo(0, 0));

function logout() {
posthog.capture(Event.SIGNED_OUT,{user:local_info}); // capture the signed out events
localStorage.clear();
navigate("/signup");
}
Expand Down
8 changes: 8 additions & 0 deletions apps/frontend/src/app/constants/PosthogEvent/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const Event = {
SIGNED_OUT:"User Signed Out",
SIGNED_IN :"User Signed In",
SIGNED_UP: "User signed Up",
BULK_LINK_CREATION :"Bulk Links Created",
LINK_CREATION :"Link Created",
}
export default Event;
13 changes: 10 additions & 3 deletions apps/frontend/src/app/pages/Auth/SignIn/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// prettier-ignore
import { Layout, Menu, Button, Row, Col, Typography, Form, Input, Switch } from "antd";

import signinbg from "app/assets/images/img-signin.jpg";
import { useEffect, useState } from "react";
import { Link, useNavigate } from "react-router-dom";
Expand All @@ -9,6 +8,8 @@ import { onFinish, onFinishFailed } from "app/utils/outputResponse.js";
import Navbar from "app/components/Navbar";
import Footer from "app/components/Footer";
import { mockUser } from "app/constants/mockData"; // TODO: Remove this line
import { usePostHog } from "posthog-js/react";
import Event from "app/constants/PosthogEvent";
function onChange(checked) {
console.log(`switch to ${checked}`);
}
Expand All @@ -18,7 +19,7 @@ const { Content } = Layout;
function SignIn() {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");

const posthog = usePostHog();
const navigate = useNavigate();

useEffect(() => {
Expand All @@ -43,7 +44,13 @@ function SignIn() {
// Set Local Storage
localStorage.setItem("user-info", JSON.stringify(result));
*/
navigate("/dashboard");
// add telemetry for Sign In
posthog.capture(Event.SIGNED_IN, {
// To DO: Add user info here after completing the api integration
email: email,
});
console.log("User Signed In");
navigate("/dashboard");
} catch (e) {
console.log(e);
}
Expand Down
16 changes: 15 additions & 1 deletion apps/frontend/src/app/pages/Auth/SignUp/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useEffect, useState } from "react";
import { Navigate, useNavigate, Link } from "react-router-dom";
// import posthog from 'posthog-js';

// prettier-ignore
import { Layout, Menu, Button, Typography, Form, Input,Checkbox,Card } from "antd";
Expand All @@ -13,10 +14,14 @@ import routes from "app/constants/Routes";
const { Title } = Typography;
const { Content } = Layout;
import Footer from "app/components/Footer";

import { usePostHog } from "posthog-js/react";
import Event from "app/constants/PosthogEvent";
function SignUp() {
const history = useNavigate();
const posthog = usePostHog(); // posthog instance
useEffect(() => {
// console.log(posthog);
// posthog.capture('$pageview'); // sent the pageview to posthog
if (localStorage.getItem("user-info")) {
history.push("/dashboard");
}
Expand All @@ -33,13 +38,22 @@ function SignUp() {
email: email,
password: password,
});

// sent the signup event to posthog
posthog.capture(Event.SIGNED_UP, {
name:name,
email:email
});

try {
// Get Response
const result = await apiUtil.getResponse(
routes.AUTH_BASE_URL + "/user/registration", // TO DO: shift to constants
reqBody
);

// posthog.capture('User signed Up', { userInfo: result }); // sent the signup event to posthog

// Set Local Storage
localStorage.setItem("user-info", JSON.stringify(result));
// Navigate to Dashboard
Expand Down
7 changes: 6 additions & 1 deletion apps/frontend/src/app/pages/CreateBulkLink/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import { Table } from "antd";
import { nanoid } from "nanoid";
import { Excel } from "antd-table-saveas-excel";
import { Form, Button } from "antd";

import { usePostHog } from "posthog-js/react";
import Event from "app/constants/PosthogEvent";
function Bulk() {
const [form] = Form.useForm();
const baseUrl = " https://yaus.xyz";
const [data, setdata] = useState([]);
const posthog = usePostHog();
const [excelData, setExcelData] = useState(null);
const len = 0;
const [state, setState] = useState({
Expand All @@ -21,6 +23,7 @@ function Bulk() {
});

const handleSubmit = (e) => {
let bulkLinkData = [];
for (let i = 0; i < data.length; i++) {
const customId = nanoid(6);
const userData = {
Expand All @@ -35,6 +38,7 @@ function Bulk() {
"Access-Control-Allow-Origin": "*",
};
console.log(userData);
bulkLinkData.push(userData);
/* console.log(baseUrl+{customHashId}); */
axios
.post(`${baseUrl}/register`, userData, { headers: headers })
Expand All @@ -46,6 +50,7 @@ function Bulk() {
console.log(data);
});
}
posthog.capture(Event.BULK_LINK_CREATION , {LinksData:bulkLinkData});
};

const onDownload = () => {
Expand Down
7 changes: 5 additions & 2 deletions apps/frontend/src/app/pages/CreateLink/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import FormAnalyticsPage from "app/pages/Form/LinkAnalyticsForm";
import FormRedirectPage from "app/pages/Form/LinkRedirectsForm";
import FormLinkDataPage from "app/pages/Form/LinkDataForm";
import FormTagsPage from "app/pages/Form/LinkTagsForm";
import { usePostHog } from "posthog-js/react";
import Event from "app/constants/PosthogEvent";
const { Footer } = Layout;
const { Step } = Steps;
const { Meta } = Card;
Expand All @@ -20,7 +22,7 @@ const FormDemo = () => {
const baseUrl = " https://yaus.xyz";
const [formLayout, setFormLayout] = useState("vertical");
const [modal1Visible, setModal1Visible] = useState(false);

const posthog = usePostHog();
const [state, setState] = useState({
userID: "0fe6ff38-fc46-11ec-b939-0242ac120001",
url: "",
Expand All @@ -42,11 +44,12 @@ const FormDemo = () => {
Authorization: "JWT fefege...",
"Access-Control-Allow-Origin": "*",
};
posthog.capture(Event.LINK_CREATION,{userData:userData}); // capture the link creation events
console.log(userData);
axios
.post(`${baseUrl}/register`, userData, { headers: headers })
.then((response) => {
console.log(response.status);
console.log("response"+response.status);
console.log(response.data.token);
console.log(`${baseUrl}/${state.customHashId}`);
});
Expand Down
3 changes: 2 additions & 1 deletion apps/frontend/src/app/pages/Form/LinkTagsForm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { InfoCircleOutlined } from "@ant-design/icons";
import Interaction from "app/components/Interaction";
import { LinkPreview } from "@dhaiwat10/react-link-preview";
import { nanoid } from "nanoid";
import { usePostHog } from "posthog-js/react";
const { Meta } = Card;
const { Footer } = Layout;
const props = {
Expand All @@ -40,7 +41,7 @@ const props = {
function FormTagsPage() {
const [selectedMenuItem, setSelectedMenuItem] = useState("useUrl");
const [isModalVisible, setIsModalVisible] = useState(false);

const posthog = usePostHog();
const [state, setState] = useState({
userID: "0fe6ff38-fc46-11ec-b939-0242ac120001",
url: "",
Expand Down
17 changes: 17 additions & 0 deletions apps/frontend/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
import { StrictMode } from "react";
import * as ReactDOM from "react-dom/client";
import { BrowserRouter } from "react-router-dom";
import { PostHogProvider} from 'posthog-js/react'
import posthog from 'posthog-js'

import "antd/dist/antd.css";
import "app/styles/main.css";
import "app/styles/responsive.css";

import App from "app/app";

const posthogKey = process.env['NX_REACT_APP_PUBLIC_POSTHOG_KEY'];
const posthogHost = process.env['NX_REACT_APP_PUBLIC_POSTHOG_HOST'];

if (posthogKey && posthogHost) {
posthog.init(posthogKey, {
api_host: posthogHost,
});
} else {
console.error("Missing PostHog environment variables");
}

const root = ReactDOM.createRoot(
document.getElementById("root") as HTMLElement
);

root.render(
<StrictMode>
<BrowserRouter>
<PostHogProvider client={posthog}>
<App />
</PostHogProvider>
</BrowserRouter>
</StrictMode>
);
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"nanoid": "^4.0.0",
"nestjs-posthog": "^1.1.2",
"nestjs-redis": "^1.3.3",
"posthog-js": "^1.76.0",
"posthog-node": "^1.3.0",
"prisma": "^3.14.0",
"prop-types": "^15.8.1",
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10993,6 +10993,11 @@ fecha@~4.2.0:
resolved "https://registry.yarnpkg.com/fecha/-/fecha-4.2.3.tgz#4d9ccdbc61e8629b259fdca67e65891448d569fd"
integrity sha512-OP2IUU6HeYKJi3i0z4A19kHMQoLVs4Hc+DPqqxI2h/DPZHTm/vjsfC6P0b4jCMy14XizLBqvndQ+UilD7707Jw==

fflate@^0.4.1:
version "0.4.8"
resolved "https://registry.yarnpkg.com/fflate/-/fflate-0.4.8.tgz#f90b82aefbd8ac174213abb338bd7ef848f0f5ae"
integrity sha512-FJqqoDBR00Mdj9ppamLa/Y7vxm+PRmNWA67N846RvsoYVMKB4q3y/de5PA7gUmRMYK/8CMz2GDZQmCRN1wBcWA==

figgy-pudding@^3.5.1:
version "3.5.2"
resolved "https://registry.yarnpkg.com/figgy-pudding/-/figgy-pudding-3.5.2.tgz#b4eee8148abb01dcf1d1ac34367d59e12fa61d6e"
Expand Down Expand Up @@ -17301,6 +17306,13 @@ postcss@^8.2.13, postcss@^8.3.5, postcss@^8.4.7:
picocolors "^1.0.0"
source-map-js "^1.0.2"

posthog-js@^1.76.0:
version "1.76.0"
resolved "https://registry.yarnpkg.com/posthog-js/-/posthog-js-1.76.0.tgz#f0f052edbe941c5af848406f921065eeaa8eaea5"
integrity sha512-C/+M+uVQ+CAEUz0ZAENMobw0fYcXGNomUXd8irelT7cQ9fsDSRgfyF5wwqaX0UXcIBD+iFXTOtK93T8NnFODyg==
dependencies:
fflate "^0.4.1"

posthog-node@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/posthog-node/-/posthog-node-1.3.0.tgz#804ed2f213a2f05253f798bf9569d55a9cad94f7"
Expand Down

0 comments on commit b281c5d

Please sign in to comment.