Browse Source

Conditional import of analytics plugins (#4327)

* made analytics plugins conditional. fixed logs URL incorrect import

* Update packages/atlas/plugins/index.ts

Co-authored-by: Artem <Artem Slugin>
Co-authored-by: Bartosz Dryl <drylbartosz@gmail.com>
attemka 1 year ago
parent
commit
1238986936

+ 2 - 0
packages/atlas/atlas.config.yml

@@ -465,6 +465,8 @@ analytics:
     id: '$VITE_USERSNAP_ID'
   GA: # Google Analytics
     id: '$VITE_GA_ID'
+  optimize:
+    id: '$VITE_OPTIMIZE_ID'
   segment: # Segment Analytics
     id: '$VITE_SEGMENT_ID'
 

+ 16 - 0
packages/atlas/plugins/index.ts

@@ -118,3 +118,19 @@ export const EmbeddedFallbackPlugin: PluginOption = {
     })
   },
 }
+
+export const OptimizePlugin: PluginOption = {
+  name: 'optimize-init-plugin',
+  transformIndexHtml: {
+    enforce: 'pre',
+    transform: (html) => {
+      const optimizeEnv = 'VITE_OPTIMIZE_ID'
+      const optimizeId = process.env[optimizeEnv] || loadEnv('production', path.join(process.cwd(), 'src'))[optimizeEnv]
+      const optimizeScript = optimizeId
+        ? `<script src="https://www.googleoptimize.com/optimize.js?id=${optimizeId}"></script>`
+        : ''
+
+      return html.replace('<optimize-script />', optimizeScript)
+    },
+  },
+}

+ 10 - 1
packages/atlas/src/.env

@@ -5,14 +5,16 @@ VITE_ENV=development
 VITE_ENV_SELECTION_ENABLED=true
 # default env in environments admin modal. Can be production, development, next or local. If not provided, VITE_ENV will be used
 VITE_DEFAULT_DATA_ENV=
-# forces maintenance screen. Set to true if Orion service is unavailable for a longer time 
+# forces maintenance screen. Set to true if Orion service is unavailable for a longer time
 VITE_FORCE_MAINTENANCE=
 
 # App configuration
 VITE_APP_ID=4414-2
 VITE_APP_NAME=Atlas
 
+
 VITE_AVATAR_SERVICE_URL=https://atlas-services.joystream.org/avatars
+VITE_ASSET_LOGS_URL=
 VITE_GEOLOCATION_SERVICE_URL=https://geolocation.joystream.org
 VITE_HCAPTCHA_SITE_KEY=41cae189-7676-4f6b-aa56-635be26d3ceb
 VITE_GOOGLE_CONSOLE_CLIENT_ID=
@@ -24,6 +26,13 @@ VITE_GOOGLE_CONSOLE_CLIENT_ID=246331758613-rc1psegmsr9l4e33nqu8rre3gno5dsca.apps
 VITE_YOUTUBE_SYNC_API_URL=https://52.204.147.11.nip.io
 VITE_YOUTUBE_COLLABORATOR_MEMBER_ID=18
 
+# Analytics tools
+VITE_GA_ID=
+VITE_SEGMENT_ID=
+VITE_SENTRY_DSN=
+VITE_OPTIMIZE_ID=
+VITE_USERSNAP_ID=
+
 # Production env URLs
 VITE_PRODUCTION_ORION_URL=https://orion.joystream.org/graphql
 VITE_PRODUCTION_QUERY_NODE_SUBSCRIPTION_URL=wss://orion.joystream.org/graphql

+ 5 - 0
packages/atlas/src/config/configSchema.ts

@@ -151,6 +151,11 @@ export const configSchema = z.object({
         id: z.string().nullable(),
       })
       .nullable(),
+    optimize: z
+      .object({
+        id: z.string().nullable(),
+      })
+      .nullable(),
     segment: z
       .object({
         id: z.string().nullable(),

+ 3 - 1
packages/atlas/src/index.html

@@ -4,6 +4,9 @@
     <meta charset="utf-8" />
     <meta name="viewport" content="width=device-width, initial-scale=1" />
 
+    <!--  The following node will be replaced by Optimize init script during Vite's build step (see ../plugins)  -->
+    <optimize-script />
+
     <!--  The following node will be replaced by meta tags (including <title>) generated during Vite's build step (see ../plugins)  -->
     <meta-tags />
 
@@ -20,7 +23,6 @@
       href="https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:wght@600;700&family=Roboto:wght@400&family=Inter:wght@400;500;600;700&display=swap"
       rel="stylesheet"
     />
-    <script src="https://www.googleoptimize.com/optimize.js?id=%VITE_OPTIMIZE_ID%"></script>
   </head>
   <body>
     <noscript>You need to enable JavaScript to run this app.</noscript>

+ 1 - 1
packages/atlas/src/providers/segmentAnalytics/segment.provider.tsx

@@ -23,7 +23,7 @@ export const SegmentAnalyticsProvider: FC<AnalyticsProviderProps> = ({ children
   const writeKey = (analyticsEnabled && atlasConfig.analytics.segment?.id) || ''
 
   const segmentAnalytics: AnalyticsContextProps = useMemo(
-    () => ({ analytics: AnalyticsBrowser.load({ writeKey }) }),
+    () => ({ analytics: writeKey ? AnalyticsBrowser.load({ writeKey }) : new AnalyticsBrowser() }),
     [writeKey]
   )
 

+ 2 - 0
packages/atlas/vite.config.ts

@@ -11,6 +11,7 @@ import {
   AtlasHtmlMetaTagsPlugin,
   AtlasWebmanifestPlugin,
   EmbeddedFallbackPlugin,
+  OptimizePlugin,
   PolkadotWorkerMetaFixPlugin,
 } from './plugins'
 
@@ -46,6 +47,7 @@ export default defineConfig({
     AtlasHtmlMetaTagsPlugin,
     AtlasWebmanifestPlugin,
     EmbeddedFallbackPlugin,
+    OptimizePlugin,
     ViteYaml(),
     react({
       exclude: /\.stories\.[tj]sx?$/,