Skip to content

Commit

Permalink
feat: GROW-284 - Refactor code and fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
hba-fingerprint committed Nov 18, 2024
1 parent 75130b5 commit 2835014
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 28 deletions.
31 changes: 10 additions & 21 deletions src/client/thirdParty/Amplitude.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import * as amplitude from '@amplitude/analytics-browser';
import { usePlaygroundSignals } from '../../app/playground/hooks/usePlaygroundSignals';
import { FunctionComponent } from 'react';
import { useVisitorData } from '@fingerprintjs/fingerprintjs-pro-react';
import { FPJS_CLIENT_TIMEOUT } from '../../const';
import { useLocation } from 'react-use';

const AMPLITUDE_INGRESS_PROXY = 'https://demo.fingerprint.com/ampl-api/2/httpapi';
const EVENT_TYPE = 'Demo Page Viewed';
Expand Down Expand Up @@ -52,47 +49,39 @@ function initAmplitude(apiKey: string) {
});
}

function initPlaygroundSignals() {
function usePlaygroundSignalsWrapper() {
usePlaygroundSignals({
onServerApiSuccess: (event) => {
const visitorId = event.products.identification?.data?.visitorId;
const botDetected = event?.products?.botd?.data?.bot?.result === 'bad' ? 'True' : 'False';

const location = useLocation();
const pagePath = location.pathname;
const pageTitle = document.title;

amplitude.add(demoPageViewedEventPropertiesEnrichment(botDetected));

amplitude.track(EVENT_TYPE, {
botDetected,
visitorId,
'Docs Page Path': pagePath,
'Docs Page Title': pageTitle,
});
},
});
}

export async function trackAskAIkHelpMethodChosen(helpMethod: string) {
const { getData: getVisitorData } = useVisitorData(
{ ignoreCache: true, timeout: FPJS_CLIENT_TIMEOUT },
{
immediate: false,
},
);

const { visitorId } = await getVisitorData({ ignoreCache: true });

export async function trackAskAIkHelpMethodChosen(
helpMethod: string,
visitorId: string,
pagePath: string,
pageTitle: string,
) {
amplitude.track(ASK_AI_CHOSEN_EVENT_TYPE, {
helpMethod,
visitorId,
'Docs Page Path': pagePath,
'Docs Page Title': pageTitle,
});
}

export const Amplitude: FunctionComponent<AmplitudeProps> = ({ apiKey }) => {
initAmplitude(apiKey);
initPlaygroundSignals();
usePlaygroundSignalsWrapper();

return null;
};
41 changes: 34 additions & 7 deletions src/client/thirdParty/Inkeep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import type {
import { env } from '../../env';
import dynamic from 'next/dynamic';
import { trackAskAIkHelpMethodChosen } from './Amplitude';
import { GetDataOptions, useVisitorData, VisitorData } from '@fingerprintjs/fingerprintjs-pro-react';
import { FPJS_CLIENT_TIMEOUT } from '../../const';
import { useLocation } from 'react-use';
import { LocationSensorState } from 'react-use/lib/useLocation';

const GET_HELP_OPTIONS_CLICKED = 'get_help_option_clicked';

Expand All @@ -29,18 +33,33 @@ type InkeepSharedSettings = {
modalSettings: InkeepModalSettings;
};

export const customAnalyticsCallback = async (event) => {
if (event.eventName === GET_HELP_OPTIONS_CLICKED) {
const { name } = event.properties;
await trackAskAIkHelpMethodChosen(name);
}
export const createCustomAnalyticsCallback = (
getVisitorData: (getDataOptions?: GetDataOptions<boolean>) => Promise<VisitorData<boolean>>,
location: LocationSensorState,
) => {
return async (event) => {
if (event.eventName === GET_HELP_OPTIONS_CLICKED) {
const { visitorId } = await getVisitorData({ ignoreCache: true });

const { name } = event.properties;
const pagePath = location.pathname || '';
const pageTitle = document.title;

await trackAskAIkHelpMethodChosen(name, visitorId, pagePath, pageTitle);
}
};
};

const useInkeepSettings = (): InkeepSharedSettings => {
const useInkeepSettings = (
getVisitorData: (getDataOptions?: GetDataOptions<boolean>) => Promise<VisitorData<boolean>>,
location: LocationSensorState,
): InkeepSharedSettings => {
const apiKey = env.NEXT_PUBLIC_INKEEP_API_KEY;
const integrationId = env.NEXT_PUBLIC_INKEEP_INTEGRATION_ID;
const organizationId = env.NEXT_PUBLIC_INKEEP_ORG_ID;

const customAnalyticsCallback = createCustomAnalyticsCallback(getVisitorData, location);

const baseSettings: InkeepBaseSettings = {
apiKey,
integrationId,
Expand Down Expand Up @@ -94,7 +113,15 @@ const useInkeepSettings = (): InkeepSharedSettings => {
};

export function InkeepChatButton() {
const { baseSettings, aiChatSettings, searchSettings, modalSettings } = useInkeepSettings();
const { getData: getVisitorData } = useVisitorData(
{ ignoreCache: true, timeout: FPJS_CLIENT_TIMEOUT },
{
immediate: false,
},
);
const location = useLocation();

const { baseSettings, aiChatSettings, searchSettings, modalSettings } = useInkeepSettings(getVisitorData, location);

const chatButtonProps: InkeepChatButtonProps = {
baseSettings,
Expand Down

0 comments on commit 2835014

Please sign in to comment.