Skip to content

Commit

Permalink
[UXE-4356] feat: add secrets segment variables in the workflows (#1597)
Browse files Browse the repository at this point in the history
  • Loading branch information
pauloSF0 authored Aug 9, 2024
1 parent 337fc7f commit 6c03aff
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 49 deletions.
1 change: 1 addition & 0 deletions .github/workflows/deploy-production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ jobs:
env:
VITE_STRIPE_TOKEN_PROD: ${{ secrets.PROD_STRIPE_TOKEN }}
VITE_RECAPTCHA_SITE_KEY: ${{ secrets.PROD_RECAPTCHA_SITE_KEY }}
VITE_SEGMENT_TOKEN: ${{ secrets.PROD_SEGMENT_TOKEN }}
NODE_ENV: production
VITE_ENVIRONMENT: production
1 change: 1 addition & 0 deletions .github/workflows/deploy-stage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ jobs:
env:
VITE_STRIPE_TOKEN_STAGE: ${{ secrets.STAGE_STRIPE_TOKEN }}
VITE_RECAPTCHA_SITE_KEY: ${{ secrets.STAGE_RECAPTCHA_SITE_KEY }}
VITE_SEGMENT_TOKEN: ${{ secrets.STAGE_SEGMENT_TOKEN }}
NODE_ENV: stage
VITE_ENVIRONMENT: stage
11 changes: 7 additions & 4 deletions src/plugins/AnalyticsTrackerAdapterPlugin.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { getEnvironment } from '@/helpers'
import { makeAnalyticsClient } from './factories/analytics-tracking-factory'
import { AnalyticsTrackerAdapter } from './analytics/AnalyticsTrackerAdapter'

import { makeSegmentToken } from './factories/segment-handler-token-factory'
/**@type {import('vue').Plugin} */
export default {
// eslint-disable-next-line no-unused-vars
install: (Vue, options) => {
const environment = getEnvironment()
const segmentToken = makeSegmentToken()

let analyticsClient = undefined

const analyticsClient = makeAnalyticsClient(environment)
if (segmentToken) {
analyticsClient = makeAnalyticsClient(segmentToken)
}

const app = Vue
const trackerInstance = new AnalyticsTrackerAdapter(analyticsClient)
Expand Down
13 changes: 10 additions & 3 deletions src/plugins/analytics/AnalyticsTrackerAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export class AnalyticsTrackerAdapter {
#events = []
/** @type {import('analytics').AnalyticsInstance} */
#analyticsClient = null
/** @type {Boolean} */
#hasAnalyticsClient = false
#traits = {}

/** @type {SignUpTracker} */
Expand All @@ -31,6 +33,7 @@ export class AnalyticsTrackerAdapter {
* @param {import('analytics').AnalyticsInstance} analyticsClient - The client for tracking.
*/
constructor(analyticsClient) {
this.#hasAnalyticsClient = !!analyticsClient
this.#analyticsClient = analyticsClient

this.#signUpTracker = new SignUpTracker(this)
Expand All @@ -48,10 +51,14 @@ export class AnalyticsTrackerAdapter {
this.#events.push(event)
}

#hasAnalytics() {
return this.#hasAnalyticsClient
}
/**
* call this method to run each stored tracker event
*/
async track() {
if (!this.#hasAnalytics()) return
this.#events.forEach(async (action) => {
const { eventName, props } = action
props.application = 'console-kit'
Expand All @@ -68,9 +75,8 @@ export class AnalyticsTrackerAdapter {
* @return {Promise<void>}
*/
async identify(id) {
if (!id) {
return
}
if (!id || !this.#hasAnalytics()) return

await this.#analyticsClient.identify(id, this.#traits)
}

Expand All @@ -79,6 +85,7 @@ export class AnalyticsTrackerAdapter {
* @param {Object} traitsToAssign - traits that should be sended with all tracking calls
*/
assignGroupTraits(traitsToAssign) {
if (!this.#hasAnalytics()) return
if (!traitsToAssign) {
throw new Error('Invalid traits provided')
}
Expand Down
27 changes: 1 addition & 26 deletions src/plugins/factories/analytics-tracking-factory.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,12 @@
import { Analytics } from 'analytics'
import segmentPlugin from '@analytics/segment'
const environment = {
development: {
segmentToken: 'EeKBQaSXEBAkaOYc0Z9XrPALzdt5rLpI'
},
stage: {
segmentToken: 'EeKBQaSXEBAkaOYc0Z9XrPALzdt5rLpI'
},
production: {
segmentToken: 'MFfoMuZo6JPehBpfh21zXEjlwOs2zDeP'
}
}

function getSegmentToken(env) {
return environment[env].segmentToken
}

/**
* Initialize analytics module.
* @param {string} environment
* @returns {import('analytics').AnalyticsInstance}
*/
export function makeAnalyticsClient(environment) {
if (!environment) {
throw Error('Provide an environment to select correct tracking token')
}
const isInvalidEnvironment = !['development', 'stage', 'production'].includes(environment)
if (isInvalidEnvironment) {
throw Error('Provide an valid environment to select correct tracking token')
}

const segmentToken = getSegmentToken(environment)

export function makeAnalyticsClient(segmentToken) {
const plugins = [segmentPlugin({ writeKey: segmentToken })]

return new Analytics({ plugins })
Expand Down
13 changes: 13 additions & 0 deletions src/plugins/factories/segment-handler-token-factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function makeSegmentToken() {
const segmentToken = import.meta.env['VITE_SEGMENT_TOKEN']

if (!segmentToken) {
// eslint-disable-next-line no-console
console.warn('Segment token is missing')
return
}

return segmentToken
}

export { makeSegmentToken }
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const makeSut = () => {
track: vi.fn(),
identify: vi.fn()
}

const sut = new AnalyticsTrackerAdapter(analyticsClientSpy)

return {
Expand Down
16 changes: 1 addition & 15 deletions src/tests/plugins/factories/analytics-tracking-factory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,10 @@ const makeSut = () => {
}

describe('AnalyticsTrackingFactory', () => {
it('should return an error whe no environment is provided', () => {
const { sut } = makeSut()

expect(() => sut(null)).toThrowError('Provide an environment to select correct tracking token')
})

it('should return an error on invalid environment provided', () => {
const { sut } = makeSut()

expect(() => sut('invalid-environment-stub')).toThrowError(
'Provide an valid environment to select correct tracking token'
)
})

it('should create analytics with correct configuration', () => {
const { sut } = makeSut()

const analyticsClient = sut('development')
const analyticsClient = sut('token_segment')

expect(analyticsClient).toBeTruthy()
})
Expand Down
36 changes: 36 additions & 0 deletions src/tests/plugins/factories/segment-handler-token-factory.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { describe, it, expect, vi, afterEach } from 'vitest'
import { makeSegmentToken } from '@/plugins/factories/segment-handler-token-factory'

afterEach(() => {
vi.unstubAllEnvs()
vi.unstubAllGlobals()
})

describe('makeSegmentToken', () => {
it('should return the production token if environment is production', () => {
vi.stubEnv('VITE_SEGMENT_TOKEN', 'prod_token_value')
const result = makeSegmentToken()

expect(result).toBe('prod_token_value')
})

it('should return the stage token if environment is not production', () => {
vi.stubEnv('VITE_SEGMENT_TOKEN', 'stage_token_value')

const result = makeSegmentToken()

expect(result).toBe('stage_token_value')
})

it('should warn and return an empty string if the token is missing', () => {
const warnMock = vi.spyOn(console, 'warn').mockImplementation(() => {})
vi.stubEnv('VITE_SEGMENT_TOKEN', '')

const result = makeSegmentToken()

expect(warnMock).toHaveBeenCalledWith('Segment token is missing')
expect(result).toBeUndefined()

warnMock.mockRestore()
})
})

0 comments on commit 6c03aff

Please sign in to comment.