0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -05:00

refactor: fix gtag reporting

This commit is contained in:
Gao Sun 2023-05-25 22:21:08 +08:00
parent ef6b1f4e66
commit 5125da67f2
No known key found for this signature in database
GPG key ID: 13EBE123E4773688
3 changed files with 21 additions and 10 deletions

View file

@ -38,7 +38,11 @@ export default function withSecurityHeaders<InputContext extends RequestContext>
const developmentOrigins = conditionalArray(!isProduction && 'ws:'); const developmentOrigins = conditionalArray(!isProduction && 'ws:');
const appInsightsOrigins = ['https://*.applicationinsights.azure.com']; const appInsightsOrigins = ['https://*.applicationinsights.azure.com'];
// Gtag will load by `<script />` // Gtag will load by `<script />`
const gtagOrigins = ['https://*.googletagmanager.com']; const gtagOrigins = [
'https://*.googletagmanager.com',
'https://*.doubleclick.net',
'https://*.googleadservices.com',
];
return async ( return async (
context: InputContext, context: InputContext,
@ -94,7 +98,7 @@ export default function withSecurityHeaders<InputContext extends RequestContext>
directives: { directives: {
'upgrade-insecure-requests': null, 'upgrade-insecure-requests': null,
imgSrc: ["'self'", 'data:', 'https:'], imgSrc: ["'self'", 'data:', 'https:'],
scriptSrc: ["'self'", "'unsafe-eval'", "'unsafe-inline'"], scriptSrc: ["'self'", "'unsafe-eval'", "'unsafe-inline'", ...gtagOrigins],
connectSrc: [ connectSrc: [
"'self'", "'self'",
...adminOrigins, ...adminOrigins,

View file

@ -81,15 +81,15 @@ function App() {
<div className={styles.app}> <div className={styles.app}>
<SWRConfig value={swrOptions}> <SWRConfig value={swrOptions}>
<AppBoundary> <AppBoundary>
<Helmet> {shouldReportToGtag && (
{shouldReportToGtag && ( <Helmet>
<script <script
async async
crossOrigin="anonymous" crossOrigin="anonymous"
src={`https://www.googletagmanager.com/gtag/js?id=${gtagAwTrackingId}`} src={`https://www.googletagmanager.com/gtag/js?id=${gtagAwTrackingId}`}
/> />
)} </Helmet>
</Helmet> )}
<Toast /> <Toast />
<Routes> <Routes>
<Route index element={<Navigate replace to={welcomePathname} />} /> <Route index element={<Navigate replace to={welcomePathname} />} />

View file

@ -6,7 +6,14 @@ import { OnboardingRoute } from './types';
export const getOnboardingPage = (page: OnboardingPage) => export const getOnboardingPage = (page: OnboardingPage) =>
joinPath(OnboardingRoute.Onboarding, page); joinPath(OnboardingRoute.Onboarding, page);
export const gtag = (...args: unknown[]) => { /** This function is updated from the Google Tag official code snippet. */
// eslint-disable-next-line @silverhand/fp/no-mutating-methods export function gtag(..._: unknown[]) {
window.dataLayer?.push(args); if (!window.dataLayer) {
}; // eslint-disable-next-line @silverhand/fp/no-mutation
window.dataLayer = [];
}
// We cannot use rest params here since gtag has some internal logic about `arguments` for data transpiling
// eslint-disable-next-line @silverhand/fp/no-mutating-methods, prefer-rest-params
window.dataLayer.push(arguments);
}