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] Track install and error events #1984

Open
wants to merge 10 commits into
base: trunk
Choose a base branch
from
75 changes: 42 additions & 33 deletions packages/playground/website/src/components/layout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
setSiteManagerOpen,
} from '../../lib/state/redux/slice-ui';
import { ImportFormModal } from '../import-form/modal';
import { logErrorEvent } from '../../lib/tracking';

acquireOAuthTokenIfNeeded();

Expand All @@ -42,8 +43,8 @@ export const modalSlugs = {
START_ERROR: 'start-error',
IMPORT_FORM: 'import-form',
GITHUB_IMPORT: 'github-import',
GITHUB_EXPORT: 'github-export'
}
GITHUB_EXPORT: 'github-export',
};

const displayMode = getDisplayModeFromQuery();
function getDisplayModeFromQuery(): DisplayMode {
Expand Down Expand Up @@ -159,6 +160,10 @@ function Modals(blueprint: Blueprint) {
useEffect(() => {
addCrashListener(logger, (e) => {
const error = e as CustomEvent;
if (error.detail?.source) {
logErrorEvent(error.detail.source);
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While testing this I realized that request errors show only a white screen without any error messages.
I think that we can improve this, so I opened an issue #1993

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to also log errors when the source is empty?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From reading the code this shouldn't happen, but let's track it to make sure it stays like that. a10dfc9


if (error.detail?.source === 'php-wasm') {
dispatch(setActiveModal(modalSlugs.ERROR_REPORT));
}
Expand All @@ -179,39 +184,43 @@ function Modals(blueprint: Blueprint) {
} else if (currentModal === modalSlugs.IMPORT_FORM) {
return <ImportFormModal />;
} else if (currentModal === modalSlugs.GITHUB_IMPORT) {
return <GithubImportModal
onImported={({
url,
path,
files,
pluginOrThemeName,
contentType,
urlInformation: { owner, repo, type, pr },
}) => {
setGithubExportValues({
repoUrl: url,
prNumber: pr?.toString(),
toPathInRepo: path,
prAction: pr ? 'update' : 'create',
return (
<GithubImportModal
onImported={({
url,
path,
files,
pluginOrThemeName,
contentType,
plugin: pluginOrThemeName,
theme: pluginOrThemeName,
});
setGithubExportFiles(files);
}}
/>;
urlInformation: { owner, repo, type, pr },
}) => {
setGithubExportValues({
repoUrl: url,
prNumber: pr?.toString(),
toPathInRepo: path,
prAction: pr ? 'update' : 'create',
contentType,
plugin: pluginOrThemeName,
theme: pluginOrThemeName,
});
setGithubExportFiles(files);
}}
/>
);
} else if (currentModal === modalSlugs.GITHUB_EXPORT) {
return <GithubExportModal
allowZipExport={
(query.get('ghexport-allow-include-zip') ?? 'yes') === 'yes'
}
initialValues={githubExportValues}
initialFilesBeforeChanges={githubExportFiles}
onExported={(prUrl, formValues) => {
setGithubExportValues(formValues);
setGithubExportFiles(undefined);
}}
/>;
return (
<GithubExportModal
allowZipExport={
(query.get('ghexport-allow-include-zip') ?? 'yes') === 'yes'
}
initialValues={githubExportValues}
initialFilesBeforeChanges={githubExportFiles}
onExported={(prUrl, formValues) => {
setGithubExportValues(formValues);
setGithubExportFiles(undefined);
}}
/>
);
}

if (query.get('gh-ensure-auth') === 'yes') {
Expand Down
23 changes: 10 additions & 13 deletions packages/playground/website/src/lib/state/redux/boot-site-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ import {
removeClientInfo,
updateClientInfo,
} from './slice-clients';
import { logTrackingEvent } from '../../tracking';
import { Blueprint, StepDefinition } from '@wp-playground/blueprints';
import {
logBlueprintStepEvent,
logErrorEvent,
logTrackingEvent,
} from '../../tracking';
import { Blueprint } from '@wp-playground/blueprints';
import { logger } from '@php-wasm/logger';
import { setupPostMessageRelay } from '@php-wasm/web';
import { startPlaygroundWeb } from '@wp-playground/client';
Expand Down Expand Up @@ -100,17 +104,6 @@ export function bootSiteClient(
blueprint = site.metadata.runtimeConfiguration;
} else {
blueprint = site.metadata.originalBlueprint;
// Log the names of provided Blueprint's steps.
// Only the names (e.g. "runPhp" or "login") are logged. Step options like
// code, password, URLs are never sent anywhere.
const steps = (blueprint?.steps || [])
?.filter(
(step: any) => !!(typeof step === 'object' && step?.step)
)
.map((step) => (step as StepDefinition).step);
for (const step of steps) {
logTrackingEvent('step', { step });
}
}

let playground: PlaygroundClient;
Expand All @@ -125,6 +118,9 @@ export function bootSiteClient(
onClientConnected: (playground) => {
(window as any)['playground'] = playground;
},
onBlueprintStepCompleted: (result, step) => {
logBlueprintStepEvent(step);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, why only log the completed steps instead of all the steps?

},
mounts: mountDescriptor
? [
{
Expand Down Expand Up @@ -177,6 +173,7 @@ export function bootSiteClient(
}
} catch (e) {
logger.error(e);
logErrorEvent('bootSiteClient');
dispatch(setActiveSiteError('site-boot-failed'));
dispatch(setActiveModal(modalSlugs.ERROR_REPORT));
return;
Expand Down
62 changes: 54 additions & 8 deletions packages/playground/website/src/lib/tracking.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,69 @@
import { StepDefinition } from '@wp-playground/blueprints';
import { logger } from '@php-wasm/logger';

/**
* Declare the global window.gtag function
*/
declare global {
interface Window { gtag: any; }
interface Window {
gtag: any;
}
}

/**
* Google Analytics event names
*/
type GAEvent = 'load' | 'step';
type GAEvent = 'load' | 'step' | 'install' | 'error';

/**
* Log a tracking event to Google Analytics
* @param GAEvent The event name
* @param Object Event data
*/
export const logTrackingEvent = (event: GAEvent, data?: {[key: string]: string}) => {
if (typeof window === 'undefined' || !window.gtag) {
return;
}
window.gtag('event', event, data);
}
export const logTrackingEvent = (
event: GAEvent,
data?: { [key: string]: string }
) => {
try {
if (typeof window === 'undefined' || !window.gtag) {
return;
}
window.gtag('event', event, data);
} catch (error) {
logger.warn('Failed to log tracking event', event, data, error);
}
};

/**
* Log Blueprint step events
* @param step The Blueprint step
*/
export const logBlueprintStepEvent = (step: StepDefinition) => {
/**
* Log the names of provided Blueprint's steps.
* Only the names (e.g. "runPhp" or "login") are logged. Step options like
* code, password, URLs are never sent anywhere.
*/
logTrackingEvent('step', { step: step.step });

if (step.step === 'installPlugin' && (step as any).pluginData.slug) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this account for blueprint.plugins? Or just blueprint.steps?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both. It will run whenever a step is completed.
During Blueprint compile, blueprint.plugins is converted to steps.

logTrackingEvent('install', {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the advantage of having a single install event with either a plugin or theme prop vs having separate installPlugin and installTheme events?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None, it will probably be easier to build reports with separate events.
I switched to two events beeb585

plugin: (step as any).pluginData.slug,
});
} else if (step.step === 'installTheme' && (step as any).themeData.slug) {
logTrackingEvent('install', {
theme: (step as any).themeData.slug,
});
}
};

/**
* Log error events
*
* @param error The error
*/
export const logErrorEvent = (source: string) => {
Copy link
Preview

Copilot AI Dec 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error source is being logged as a string, which can lead to inconsistencies. Consider using an enum or predefined constants for the error sources to ensure consistency.

Suggested change
export const logErrorEvent = (source: string) => {
export const logErrorEvent = (source: ErrorSource) => {

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
logTrackingEvent('error', {
source,
});
};