Skip to content

Commit 0348d50

Browse files
committed
wip
1 parent 5caf4d6 commit 0348d50

File tree

4 files changed

+134
-6
lines changed

4 files changed

+134
-6
lines changed

.github/workflows/ci.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,33 @@ env:
55
HUSKY: 0
66
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: 1
77
jobs:
8+
deploy:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- uses: actions/checkout@v3
13+
14+
- name: Setup Node
15+
uses: actions/setup-node@v3
16+
with:
17+
node-version: '20.x'
18+
cache: 'yarn'
19+
20+
- name: Installing Dependencies
21+
run: yarn install
22+
23+
- name: Building App
24+
run: yarn build
25+
env:
26+
REACT_APP_SEGMENT_WRITE_KEY: ${{ secrets.REACT_APP_SEGMENT_WRITE_KEY }}
27+
28+
- name: Deploy with gh-pages
29+
run: |
30+
git remote set-url origin https://git:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git
31+
npx gh-pages -d build -u "github-actions-bot <[email protected]>"
32+
env:
33+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
34+
835
analytics-node:
936
name: 'analytics-node QA (Node.js v${{ matrix.node-version }})'
1037
runs-on: ubuntu-latest
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Deploy Signals Example to GH pages
2+
3+
on:
4+
push:
5+
branches:
6+
# - main
7+
- deploy-signals-example
8+
9+
jobs:
10+
deploy:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v3
15+
16+
- name: Setup Node
17+
uses: actions/setup-node@v3
18+
with:
19+
node-version: '20.x'
20+
cache: 'yarn'
21+
22+
- name: Change Directory to signals-example
23+
run: cd packages/signals/signals-example
24+
25+
- name: Installing Dependencies
26+
run: yarn install
27+
working-directory: packages/signals/signals-example
28+
29+
- name: Building App
30+
run: yarn build
31+
env:
32+
REACT_APP_SEGMENT_WRITE_KEY: ${{ secrets.REACT_APP_SEGMENT_WRITE_KEY }}
33+
working-directory: packages/signals/signals-example
34+
35+
- name: Deploy with gh-pages
36+
run: |
37+
git remote set-url origin https://git:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git
38+
npx gh-pages -d build -u "github-actions-bot <[email protected]>"
39+
env:
40+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
41+
working-directory: packages/signals/signals-example

packages/signals/signals-example/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "@internal/signals-example",
33
"private": true,
4+
"homepage": "https://segmentio.github.io/<repository-name>/my-test-site",
45
"scripts": {
56
".": "yarn run -T turbo run --filter=@internal/signals-example...",
67
"dev": "webpack serve",

packages/signals/signals-example/src/lib/analytics.ts

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,72 @@
44
import { AnalyticsBrowser } from '@segment/analytics-next'
55
import { SignalsPlugin, ProcessSignal } from '@segment/analytics-signals'
66

7-
export const analytics = new AnalyticsBrowser()
8-
if (!process.env.WRITEKEY) {
7+
// Function to get query string parameters
8+
9+
const STORAGE_TYPE: 'localStorage' | 'sessionStorage' = 'localStorage'
10+
11+
// Function to set localStorage from query string parameters
12+
13+
const queryParams = {
14+
STAGE_KEY_NAME: 'stage',
15+
WRITE_KEY_NAME: 'wk',
16+
RESET_KEY_NAME: 'reset',
17+
}
18+
19+
const Storage = () => {
20+
const getQueryParams = () => {
21+
const params = new URLSearchParams(window.location.search)
22+
const stage = params.get(queryParams.STAGE_KEY_NAME)
23+
const writeKey = params.get(queryParams.WRITE_KEY_NAME)
24+
const reset = params.get(queryParams.RESET_KEY_NAME)
25+
return { stage, writeKey, reset }
26+
}
27+
28+
const clearStorage = () => {
29+
window[STORAGE_TYPE].removeItem(queryParams.STAGE_KEY_NAME)
30+
window[STORAGE_TYPE].removeItem(queryParams.WRITE_KEY_NAME)
31+
}
32+
const setStorage = () => {
33+
const { stage, writeKey, reset } = getQueryParams()
34+
35+
if (stage !== null) {
36+
window[STORAGE_TYPE].setItem(queryParams.STAGE_KEY_NAME, stage)
37+
}
38+
if (writeKey !== null) {
39+
window[STORAGE_TYPE].setItem(queryParams.WRITE_KEY_NAME, writeKey)
40+
}
41+
if (reset !== null) {
42+
clearStorage()
43+
}
44+
}
45+
46+
setStorage()
47+
return {
48+
getStorage: () => {
49+
return {
50+
stage: window[STORAGE_TYPE].getItem(queryParams.STAGE_KEY_NAME),
51+
writeKey: window[STORAGE_TYPE].getItem(queryParams.WRITE_KEY_NAME),
52+
}
53+
},
54+
}
55+
}
56+
57+
// Set localStorage from query string parameters
58+
const storage = Storage()
59+
60+
// Retrieve values from localStorage or fall back to environment variables
61+
const isStage =
62+
storage.getStorage().stage === 'true' || process.env.STAGE === 'true'
63+
const writeKey = storage.getStorage().writeKey || process.env.WRITEKEY
64+
65+
if (!writeKey) {
966
throw new Error('No writekey provided.')
1067
}
1168

69+
console.log('Query params allowed:', JSON.stringify(Object.values(queryParams)))
70+
71+
export const analytics = new AnalyticsBrowser()
72+
1273
const processSignalExample: ProcessSignal = (
1374
signal,
1475
{ analytics, signals }
@@ -29,8 +90,6 @@ const processSignalExample: ProcessSignal = (
2990
}
3091
}
3192

32-
const isStage = process.env.STAGE === 'true'
33-
3493
const signalsPlugin = new SignalsPlugin({
3594
...(isStage ? { apiHost: 'signals.segment.build/v1' } : {}),
3695
// enableDebugLogging: true,
@@ -41,7 +100,7 @@ export const loadAnalytics = () =>
41100
analytics
42101
.load(
43102
{
44-
writeKey: process.env.WRITEKEY!,
103+
writeKey: writeKey,
45104
plugins: [signalsPlugin],
46105
...(isStage ? { cdnURL: 'https://cdn.segment.build' } : {}),
47106
},
@@ -58,7 +117,7 @@ export const loadAnalytics = () =>
58117
}
59118
)
60119
.then(() => {
61-
console.log(`Analytics loaded with WRITEKEY=${process.env.WRITEKEY}`)
120+
console.log(`Analytics loaded with WRITEKEY=${writeKey}`)
62121
// @ts-ignore
63122
window.analytics = analytics // for debugging
64123
})

0 commit comments

Comments
 (0)