Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit d0fffb3

Browse files
authored
Merge pull request #10 from topcoder-platform/feature/segment-analytics
feat: init Segment Analytics
2 parents 0824510 + e43175f commit d0fffb3

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

README.md

+23-5
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ This `micro-frontends-frame` app has 2 types of configs:
3535
}
3636
}
3737
```
38-
38+
3939
ii. Location of the AWS S3 files:
4040
- Configure micro app names and relative URL to be used when deployed to production environment in file at location : `https://tc-platform-prod.s3.amazonaws.com/micro-frontends/micro-frontends-config-production.json`
4141
- Configure micro app names and relative URL to be used when deployed to development environment in file at location : `https://tc-platform-dev.s3.amazonaws.com/micro-frontends/micro-frontends-config-development.json`
4242
- Configure micro app names and relative URL to be used when deployed to local environment in file at location : `./micro-frontends-frame/config/micro-frontends-config-local.json`
43-
43+
4444

4545
2. Route mapping handled by the frame, containing `route path` and `micro app name` for each micro app. The configuration files are available on TC AWS S3 and have public access.
4646

@@ -54,7 +54,7 @@ This `micro-frontends-frame` app has 2 types of configs:
5454
- Configure route path and micro app name to be used when deployed to production environment in file at location : `https://tc-platform-prod.s3.amazonaws.com/micro-frontends/micro-frontends-routes-production.txt`
5555
- Configure route path and micro app name to be used when deployed to development environment in file at location : `https://tc-platform-dev.s3.amazonaws.com/micro-frontends/micro-frontends-routes-development.txt`
5656
- Configure route path and micro app name to be used when deployed to development environment in file at location : `./micro-frontends-frame/config/micro-frontends-routes-local.txt`
57-
57+
5858
⚠️ **NOTE** : When a configuration files is updated on TC AWS S3, make sure to give public access to the file.
5959

6060
## NPM Commands
@@ -133,11 +133,29 @@ Make sure you have [Heroky CLI](https://devcenter.heroku.com/articles/heroku-cli
133133
- `git push heroku master` - push changes to Heroku and trigger deploying
134134
- ⚠️ **NOTE** : Authorization would not work because only predefined list of domain allowed by `accounts-app`.
135135

136+
## Segment Analytics
137+
138+
Because analytics can be normally initialized once per website, we are initializing Segment Analytics inside the Frame app instead of each child app separately. See [Segment Analytics Quick Guide](https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/quickstart/).
139+
140+
- Analytics requires `SEGMENT_ANALYTICS_KEY` to work.
141+
- It should be set as environment variable `SEGMENT_ANALYTICS_KEY` during running `npm run build` (for production build) or `npm run local-client` (for local development).
142+
- If `SEGMENT_ANALYTICS_KEY` environment variable is not set during the build process, the Segment Analytics would not be initialized.
143+
- Analytics would be exposed to the `window` object as `window.analytics`. See [Segment Analytics API Guide](https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/).
144+
- Note, that because we can build the Frame app without analytics initialized, all the child apps which use analytics should always check if it's initialized before usage, for example like this:
145+
```js
146+
if (window.analytics && typeof window.analytics.page === "function") {
147+
window.analytics.page();
148+
}
149+
```
150+
- Each child app should take care about calling `window.analytics.page()` each time the page is changed. We can pass [additional arguments](https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/#page) to this method if needed.
151+
- We can enable debug mode by calling `analytics.debug();` inside browser console, see [debug documentation](https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/#debug).
152+
- TODO: we might consider implementing one global logic for calling `window.analytics.page()` inside Frame of Navbar app so child apps wouldn't worry about this. Though we have to make sure that it works smoothly. In particular we have to make sure that if child app updates page `<title>` then `window.analytics.page()` is called after that and logs to the analytic correct page title. Also, child apps might want to provide additional arguments when calling `window.analytics.page()`. So we might come to this improvement in some time, after we try the current approach.
153+
136154
## Add/Remove child app
137155

138156
For adding a child app to the root app make the following steps:
139157

140-
1. Add child app path to importmap. File underpath
158+
1. Add child app path to importmap. File underpath
141159
- `https://tc-platform-prod.s3.amazonaws.com/micro-frontends/micro-frontends-config-production.json` for production deployment
142160
- `https://tc-platform-dev.s3.amazonaws.com/micro-frontends/micro-frontends-config-development.json` for development deployment
143161
- `./micro-frontends-frame/config/micro-frontends-config-local.json` for local deployment
@@ -154,7 +172,7 @@ For adding a child app to the root app make the following steps:
154172
"@topcoder/micro-frontends-angular-app": "//localhost:4200/topcoder-micro-frontends-angular-app.js"
155173
```
156174

157-
2. Add a route which should show the app. File underpath
175+
2. Add a route which should show the app. File underpath
158176
- `https://tc-platform-prod.s3.amazonaws.com/micro-frontends/micro-frontends-routes-production.txt` for production deployment
159177
- `https://tc-platform-dev.s3.amazonaws.com/micro-frontends/micro-frontends-routes-development.txt` for development deployment
160178
- `./micro-frontends-frame/config/micro-frontends-routes-local.txt` for local deployment

src/index.ejs

+13-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
If you need to support Angular applications, uncomment the script tag below to ensure only one instance of ZoneJS is loaded
5353
Learn more about why at https://single-spa.js.org/docs/ecosystem-angular/#zonejs
5454
-->
55-
55+
5656
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/zone.min.js"></script>
5757

5858
<% if (htmlWebpackPlugin.options.templateParameters.isLocal) { %>
@@ -66,7 +66,7 @@
6666
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/extras/amd.min.js"></script>
6767
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/extras/named-exports.min.js"></script>
6868
<% } %>
69-
69+
7070
<template id="single-spa-layout">
7171
<single-spa-router>
7272
<nav>
@@ -75,6 +75,17 @@
7575
<div id="single-spa-main"></div>
7676
</single-spa-router>
7777
</template>
78+
79+
<% if (!!htmlWebpackPlugin.options.templateParameters.SEGMENT_ANALYTICS_KEY) { %>
80+
<!-- Segment Analytics Code -->
81+
<script type="text/javascript">
82+
!function(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","debug","page","once","off","on","addSourceMiddleware","addIntegrationMiddleware","setAnonymousId","addDestinationMiddleware"];analytics.factory=function(t){return function(){var e=Array.prototype.slice.call(arguments);e.unshift(t);analytics.push(e);return analytics}};for(var t=0;t<analytics.methods.length;t++){var e=analytics.methods[t];analytics[e]=analytics.factory(e)}analytics.load=function(t,e){var n=document.createElement("script");n.type="text/javascript";n.async=!0;n.src="https://cdn.segment.com/analytics.js/v1/"+t+"/analytics.min.js";var a=document.getElementsByTagName("script")[0];a.parentNode.insertBefore(n,a);analytics._loadOptions=e};analytics.SNIPPET_VERSION="4.1.0";
83+
analytics.load("<%= htmlWebpackPlugin.options.templateParameters.SEGMENT_ANALYTICS_KEY %>");
84+
// analytics.page(); - don't call the page, each app should call it when it loads a page by itself
85+
}}();
86+
</script>
87+
<!-- // end: Segment Analytics Code -->
88+
<% } %>
7889
</head>
7990
<body>
8091
<script>

webpack.config.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* global process */
12
const webpackMerge = require("webpack-merge");
23
const singleSpaDefaults = require("webpack-config-single-spa");
34
const HtmlWebpackPlugin = require("html-webpack-plugin");
@@ -32,6 +33,7 @@ module.exports = (webpackConfigEnv) => {
3233
template: "src/index.ejs",
3334
templateParameters: {
3435
env: webpackConfigEnv.config,
36+
SEGMENT_ANALYTICS_KEY: process.env.SEGMENT_ANALYTICS_KEY,
3537
orgName,
3638
},
3739
}),

0 commit comments

Comments
 (0)