From 1224489bab90ddfaef4a640a71fdce168ec5072a Mon Sep 17 00:00:00 2001 From: namansleeps <122260931+namansleeps@users.noreply.github.com> Date: Thu, 28 Sep 2023 11:54:06 +0530 Subject: [PATCH] Analytics login (#1258) --- gui/pages/_app.js | 11 ++++++++--- gui/pages/api/apiConfig.js | 10 ++++++++++ gui/utils/utils.js | 17 ++++++++++++++++- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/gui/pages/_app.js b/gui/pages/_app.js index ad98f09bc..9b5006d9d 100644 --- a/gui/pages/_app.js +++ b/gui/pages/_app.js @@ -22,7 +22,7 @@ import { } from "@/pages/api/DashboardService"; import {useRouter} from 'next/router'; import querystring from 'querystring'; -import {refreshUrl, loadingTextEffect, getUTMParametersFromURL, setLocalStorageValue, getUserClick} from "@/utils/utils"; +import {refreshUrl, loadingTextEffect, getUTMParametersFromURL, setLocalStorageValue, getUserClick, sendGAEvent} from "@/utils/utils"; import MarketplacePublic from "./Content/Marketplace/MarketplacePublic" import {toast} from "react-toastify"; import mixpanel from 'mixpanel-browser'; @@ -125,9 +125,14 @@ export default function App() { let first_login = parsedParams.first_time_login || false const utmParams = getUTMParametersFromURL(); - if (utmParams) + if (utmParams) { sessionStorage.setItem('utm_source', utmParams.utm_source); + sessionStorage.setItem('utm_medium', utmParams.utm_medium); + sessionStorage.setItem('campaign', utmParams.utm_campaign); + } const signupSource = sessionStorage.getItem('utm_source'); + const signupMedium = sessionStorage.getItem('utm_medium'); + const singupCampaign = sessionStorage.getItem('campaign'); if (typeof window !== 'undefined' && access_token) { localStorage.setItem('accessToken', access_token); @@ -136,6 +141,7 @@ export default function App() { validateAccessToken() .then((response) => { setUserName(response.data.name || ''); + sendGAEvent(response.data.email, 'Signed Up Successfully', {'utm_source': signupSource || '', 'utm_medium': signupMedium || '', 'campaign': singupCampaign || ''}) if(mixpanelId()) mixpanel.identify(response.data.email) if(first_login) @@ -217,7 +223,6 @@ export default function App() { const handleSignUpSource = (signup) => { getFirstSignup(signup) .then((response) => { - sessionStorage.removeItem('utm_source'); }) .catch((error) => { console.error('Error validating source:', error); diff --git a/gui/pages/api/apiConfig.js b/gui/pages/api/apiConfig.js index 8f6338e0c..2f9e736b7 100644 --- a/gui/pages/api/apiConfig.js +++ b/gui/pages/api/apiConfig.js @@ -2,6 +2,8 @@ import axios from 'axios'; const GITHUB_CLIENT_ID = process.env.GITHUB_CLIENT_ID; const API_BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost:8001'; +const GOOGLE_ANALYTICS_MEASUREMENT_ID = process.env.GOOGLE_ANALYTICS_MEASUREMENT_ID; +const GOOGLE_ANALYTICS_API_SECRET = process.env.GOOGLE_ANALYTICS_API_SECRET; const MIXPANEL_AUTH_ID = process.env.MIXPANEL_AUTH_ID export const baseUrl = () => { @@ -12,6 +14,14 @@ export const githubClientId = () => { return GITHUB_CLIENT_ID; }; +export const analyticsMeasurementId = () => { + return GOOGLE_ANALYTICS_MEASUREMENT_ID; +}; + +export const analyticsApiSecret = () => { + return GOOGLE_ANALYTICS_API_SECRET; +}; + export const mixpanelId = () => { return MIXPANEL_AUTH_ID; }; diff --git a/gui/utils/utils.js b/gui/utils/utils.js index e0abc172e..b62843638 100644 --- a/gui/utils/utils.js +++ b/gui/utils/utils.js @@ -1,6 +1,6 @@ import {formatDistanceToNow, format, addMinutes} from 'date-fns'; import {utcToZonedTime} from 'date-fns-tz'; -import {baseUrl, mixpanelId} from "@/pages/api/apiConfig"; +import {baseUrl, analyticsMeasurementId, analyticsApiSecret, mixpanelId} from "@/pages/api/apiConfig"; import {EventBus} from "@/utils/eventBus"; import JSZip from "jszip"; import moment from 'moment'; @@ -566,4 +566,19 @@ export const getUserClick = (event, props) => { if(env === 'PROD' && mixpanelId()){ mixpanel.track(event, props) } +} + +export const sendGAEvent = async (client, eventName, params) => { + const measurement_id = analyticsMeasurementId(); + const api_secret = analyticsApiSecret(); + await fetch(`https://www.google-analytics.com/mp/collect?measurement_id=${measurement_id}&api_secret=${api_secret}`, { + method: "POST", + body: JSON.stringify({ + client_id: client, + events: [{ + name: eventName, + params: params + }] + }) + }); } \ No newline at end of file