Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Analytics login #1258

Merged
merged 4 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions gui/pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions gui/pages/api/apiConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand All @@ -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;
};
Expand Down
17 changes: 16 additions & 1 deletion gui/utils/utils.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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
}]
})
});
}
Loading