Skip to content

Commit

Permalink
analytics: fix url info logged during opt in event
Browse files Browse the repository at this point in the history
  • Loading branch information
alecananian committed Nov 16, 2023
1 parent 05384ca commit d479bb3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
8 changes: 6 additions & 2 deletions ui/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ import Dialog from '@/components/Dialog';
import useIsStandaloneMode from '@/logic/useIsStandaloneMode';
import EmojiPicker from '@/components/EmojiPicker';
import SettingsDialog from '@/components/Settings/SettingsDialog';
import { captureAnalyticsEvent, captureError } from '@/logic/analytics';
import {
ANALYTICS_DEFAULT_PROPERTIES,
captureAnalyticsEvent,
captureError,
} from '@/logic/analytics';
import GroupChannel from '@/groups/GroupChannel';
import PrivacyNotice from '@/groups/PrivacyNotice';
import ActivityModal, { ActivityChecker } from '@/components/ActivityModal';
Expand Down Expand Up @@ -804,7 +808,7 @@ function RoutedApp() {

useEffect(() => {
if (posthog && analyticsId !== '' && logActivity) {
posthog.identify(analyticsId);
posthog.identify(analyticsId, ANALYTICS_DEFAULT_PROPERTIES);
}
}, [posthog, analyticsId, logActivity]);

Expand Down
9 changes: 7 additions & 2 deletions ui/src/components/ActivityModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {
import { useGroups } from '@/state/groups';
import React, { useEffect } from 'react';
import { isHosted } from '@/logic/utils';
import { analyticsClient } from '@/logic/analytics';
import {
ANALYTICS_DEFAULT_PROPERTIES,
analyticsClient,
} from '@/logic/analytics';
import { PrivacyContents } from '@/groups/PrivacyNotice';
import Dialog from './Dialog';

Expand All @@ -24,7 +27,9 @@ export function ActivityChecker() {
useEffect(() => {
// manage analytics opt-in/out based on settings
if (analyticsClient.has_opted_out_capturing() && logActivity) {
analyticsClient.opt_in_capturing();
analyticsClient.opt_in_capturing({
capture_properties: ANALYTICS_DEFAULT_PROPERTIES,
});
} else if (analyticsClient.has_opted_in_capturing() && !logActivity) {
analyticsClient.opt_out_capturing();
}
Expand Down
46 changes: 25 additions & 21 deletions ui/src/logic/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,30 @@ posthog.init(import.meta.env.VITE_POSTHOG_KEY, {

export const analyticsClient = posthog;

export const ANALYTICS_DEFAULT_PROPERTIES: Properties = {
// The following default properties stop PostHog from auto-logging the URL,
// which can inadvertently reveal private info on Urbit
$current_url: null,
$pathname: null,
$set_once: null,
$host: null,
$referrer: null,
$initial_current_url: null,
$initial_referrer_url: null,
$referring_domain: null,
$initial_referring_domain: null,
$unset: [
'initial_referrer_url',
'initial_referring_domain',
'initial_current_url',
'current_url',
'pathname',
'host',
'referrer',
'referring_domain',
],
};

// Once someone is opted in this will fire no matter what so we need
// additional guarding here to prevent accidentally capturing data.
export const captureAnalyticsEvent = (
Expand All @@ -64,27 +88,7 @@ export const captureAnalyticsEvent = (
log('Attempting to capture analytics event', name);
const captureProperties: Properties = {
...(properties || {}),
// The following default properties stop PostHog from auto-logging the URL,
// which can inadvertently reveal private info on Urbit
$current_url: null,
$pathname: null,
$set_once: null,
$host: null,
$referrer: null,
$initial_current_url: null,
$initial_referrer_url: null,
$referring_domain: null,
$initial_referring_domain: null,
$unset: [
'initial_referrer_url',
'initial_referring_domain',
'initial_current_url',
'current_url',
'pathname',
'host',
'referrer',
'referring_domain',
],
...ANALYTICS_DEFAULT_PROPERTIES,
};

posthog.capture(name, captureProperties, {
Expand Down

0 comments on commit d479bb3

Please sign in to comment.