-
Notifications
You must be signed in to change notification settings - Fork 305
[Analytics] Track install and error events #1984
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
Open
bgrgicak
wants to merge
14
commits into
trunk
Choose a base branch
from
add/error-and-install-logs-to-ga
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
acea593
Track install and error events
bgrgicak 4592b34
Don't track URLs and try sending event
bgrgicak 1fa774d
Rename boot to bootSiteClient
bgrgicak ad63c49
Don't send error if source is unknown
bgrgicak a10dfc9
Track errors with unknown source
bgrgicak beeb585
Track theme and plugin installs as separate events
bgrgicak 4bc154e
Remove unnecessary nullish operator
bgrgicak 718155a
Merge branch 'trunk' into add/error-and-install-logs-to-ga
bgrgicak b6084d1
Merge branch 'trunk' into add/error-and-install-logs-to-ga
bgrgicak 165153d
Merge branch 'trunk' into add/error-and-install-logs-to-ga
bgrgicak 0d4d24f
Merge branch 'trunk' into add/error-and-install-logs-to-ga
bgrgicak 996db94
Log all steps not just completed steps
bgrgicak f628f6c
Ensure plugin shorthand installs are also logged
bgrgicak b88d3b9
Add functions for theme and plugin events
bgrgicak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,113 @@ | ||
import { Blueprint, isStepDefinition } 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' | 'installPlugin' | 'installTheme' | '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 error events | ||
* | ||
* @param error The error | ||
*/ | ||
export const logErrorEvent = (source: string) => { | ||
logTrackingEvent('error', { | ||
source, | ||
}); | ||
}; | ||
|
||
/** | ||
* Log plugin install events | ||
* @param slug The plugin slug | ||
*/ | ||
export const logPluginInstallEvent = (slug: string) => { | ||
logTrackingEvent('installPlugin', { | ||
plugin: slug, | ||
}); | ||
}; | ||
|
||
/** | ||
* Log theme install events | ||
* @param slug The theme slug | ||
*/ | ||
export const logThemeInstallEvent = (slug: string) => { | ||
logTrackingEvent('installTheme', { | ||
theme: slug, | ||
}); | ||
}; | ||
|
||
/** | ||
* Log Blueprint events | ||
* @param blueprint The Blueprint | ||
*/ | ||
export const logBlueprintEvents = (blueprint: Blueprint) => { | ||
/** | ||
* Log the names of provided Blueprint steps. | ||
* Only the names (e.g. "runPhp" or "login") are logged. Step options like | ||
* code, password, URLs are never sent anywhere. | ||
* | ||
* For installPlugin and installTheme, the plugin/theme slug is logged. | ||
*/ | ||
if (blueprint.steps) { | ||
for (const step of blueprint.steps) { | ||
if (!isStepDefinition(step)) { | ||
continue; | ||
} | ||
logTrackingEvent('step', { step: step.step }); | ||
if ( | ||
step.step === 'installPlugin' && | ||
(step as any).pluginData.slug | ||
) { | ||
logPluginInstallEvent((step as any).pluginData.slug); | ||
} else if ( | ||
step.step === 'installTheme' && | ||
(step as any).themeData.slug | ||
) { | ||
logThemeInstallEvent((step as any).themeData.slug); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Because the Blueprint isn't compiled, we need to log the plugins | ||
* that are installed using the `plugins` shorthand. | ||
*/ | ||
if (blueprint.plugins) { | ||
for (const plugin of blueprint.plugins) { | ||
if (typeof plugin !== 'string') { | ||
continue; | ||
} | ||
logTrackingEvent('step', { step: 'installPlugin' }); | ||
logPluginInstallEvent(plugin); | ||
} | ||
} | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.