Skip to content

Commit

Permalink
chore: merge branch 'master' into oscb/traitsFix
Browse files Browse the repository at this point in the history
  • Loading branch information
oscb committed Sep 29, 2023
2 parents 0e8a7ae + 32da78b commit 8391e3a
Show file tree
Hide file tree
Showing 27 changed files with 179 additions and 206 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-peaches-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-next': minor
---

Set timezone and allow userAgentData to be overridden
6 changes: 6 additions & 0 deletions .changeset/forty-toes-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@segment/analytics-consent-tools': patch
'@segment/analytics-consent-wrapper-onetrust': patch
---

Support older browsers
6 changes: 0 additions & 6 deletions .changeset/mean-apricots-hammer.md

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
key: ${{ runner.os }}-turbo-${{ github.sha }}
restore-keys: |
${{ runner.os }}-turbo-
- run: yarn turbo run --filter='consent-tools-integration-tests' test:intg
- run: yarn turbo run --filter='consent-tools-integration-tests' test:int



2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"prettier": "^2.6.2",
"ts-jest": "^28.0.4",
"ts-node": "^10.8.0",
"turbo": "^1.3.1",
"turbo": "^1.10.14",
"typescript": "^4.7.0",
"webpack": "^5.76.0",
"webpack-dev-server": "^4.15.1"
Expand Down
10 changes: 10 additions & 0 deletions packages/browser/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# @segment/analytics-next

## 1.56.0

### Minor Changes

- [#928](https://github.com/segmentio/analytics-next/pull/928) [`7f4232c`](https://github.com/segmentio/analytics-next/commit/7f4232cbdb60a4475c565e5d262b25182e47baf4) Thanks [@oscb](https://github.com/oscb)! - Adds `globalAnalyticsKey` option for setting custom global window buffers

### Patch Changes

- [#949](https://github.com/segmentio/analytics-next/pull/949) [`fcf42f6`](https://github.com/segmentio/analytics-next/commit/fcf42f68b4226b55417bbaeb6305d33129ede96d) Thanks [@silesky](https://github.com/silesky)! - Fix regression where we no longer export UniversalStorage (used in destinations)

## 1.55.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@segment/analytics-next",
"version": "1.55.0",
"version": "1.56.0",
"repository": {
"type": "git",
"url": "https://github.com/segmentio/analytics-next",
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/generated/version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// This file is generated.
export const version = '1.55.0'
export const version = '1.56.0'
1 change: 1 addition & 0 deletions packages/browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export * from './core/user'
export type { AnalyticsSnippet } from './browser/standalone-interface'
export type { MiddlewareFunction } from './plugins/middleware'
export { getGlobalAnalytics } from './lib/global-analytics-helper'
export { UniversalStorage, Store, StorageObject } from './core/storage'
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import { pick } from '../../../lib/pick'
import { SegmentioSettings } from '../../segmentio'
import { version } from '../../../generated/version'
import { CoreExtraContext } from '@segment/analytics-core'
import { UADataValues } from '../../../lib/client-hints/interfaces'
import {
highEntropyTestData,
lowEntropyTestData,
} from '../../../test-helpers/fixtures/client-hints'

let ajs: Analytics

Expand Down Expand Up @@ -262,6 +267,29 @@ describe('pageDefaults', () => {
describe('Other visitor metadata', () => {
let options: SegmentioSettings
let analytics: Analytics
;(window.navigator as any).userAgentData = {
...lowEntropyTestData,
getHighEntropyValues: jest
.fn()
.mockImplementation((hints: string[]): Promise<UADataValues> => {
let result = {}
Object.entries(highEntropyTestData).forEach(([k, v]) => {
if (hints.includes(k)) {
result = {
...result,
[k]: v,
}
}
})
return Promise.resolve({
...lowEntropyTestData,
...result,
})
}),
toJSON: jest.fn(() => {
return lowEntropyTestData
}),
}

const amendSearchParams = (search?: any): CoreExtraContext => ({
page: { search },
Expand All @@ -283,6 +311,11 @@ describe('Other visitor metadata', () => {
}
})

it('should add .timezone', async () => {
const ctx = await analytics.track('test')
assert(typeof ctx.event.context?.timezone === 'string')
})

it('should add .library', async () => {
const ctx = await analytics.track('test')
assert(ctx.event.context?.library)
Expand Down Expand Up @@ -313,6 +346,11 @@ describe('Other visitor metadata', () => {
assert(userAgent1 === userAgent2)
})

it('should add .userAgentData when available', async () => {
const ctx = await analytics.track('event')
expect(ctx.event.context?.userAgentData).toEqual(lowEntropyTestData)
})

it('should add .locale', async () => {
const ctx = await analytics.track('test')
assert(ctx.event.context?.locale === navigator.language)
Expand Down
19 changes: 18 additions & 1 deletion packages/browser/src/plugins/page-enrichment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { tld } from '../../core/user/tld'
import { gracefulDecodeURIComponent } from '../../core/query-string/gracefulDecodeURIComponent'
import { CookieStorage, UniversalStorage } from '../../core/storage'
import { Analytics } from '../../core/analytics'
import { clientHints } from '../../lib/client-hints'
import { UADataValues } from '../../lib/client-hints/interfaces'

interface PageDefault {
[key: string]: unknown
Expand Down Expand Up @@ -174,13 +176,21 @@ function referrerId(

class PageEnrichmentPlugin implements Plugin {
private instance!: Analytics
private userAgentData: UADataValues | undefined

name = 'Page Enrichment'
type: PluginType = 'before'
version = '0.1.0'
isLoaded = () => true
load = (_ctx: Context, instance: Analytics) => {
load = async (_ctx: Context, instance: Analytics) => {
this.instance = instance
try {
this.userAgentData = await clientHints(
this.instance.options.highEntropyValuesClientHints
)
} catch (_) {
// if client hints API doesn't return anything leave undefined
}
return Promise.resolve()
}

Expand Down Expand Up @@ -213,6 +223,7 @@ class PageEnrichmentPlugin implements Plugin {
const query: string = evtCtx.page.search || ''

evtCtx.userAgent = navigator.userAgent
evtCtx.userAgentData = this.userAgentData

// @ts-ignore
const locale = navigator.userLanguage || navigator.language
Expand Down Expand Up @@ -241,6 +252,12 @@ class PageEnrichmentPlugin implements Plugin {
this.instance.options.disableClientPersistence ?? false
)

try {
evtCtx.timezone = Intl.DateTimeFormat().resolvedOptions().timeZone
} catch (_) {
// If browser doesn't have support leave timezone undefined
}

return ctx
}

Expand Down
36 changes: 0 additions & 36 deletions packages/browser/src/plugins/segmentio/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ import { Analytics } from '../../../core/analytics'
import { Plugin } from '../../../core/plugin'
import { pageEnrichment } from '../../page-enrichment'
import cookie from 'js-cookie'
import { UADataValues } from '../../../lib/client-hints/interfaces'
import {
highEntropyTestData,
lowEntropyTestData,
} from '../../../test-helpers/fixtures/client-hints'

jest.mock('unfetch', () => {
return jest.fn()
Expand All @@ -24,29 +19,6 @@ describe('Segment.io', () => {
beforeEach(async () => {
jest.resetAllMocks()
jest.restoreAllMocks()
;(window.navigator as any).userAgentData = {
...lowEntropyTestData,
getHighEntropyValues: jest
.fn()
.mockImplementation((hints: string[]): Promise<UADataValues> => {
let result = {}
Object.entries(highEntropyTestData).forEach(([k, v]) => {
if (hints.includes(k)) {
result = {
...result,
[k]: v,
}
}
})
return Promise.resolve({
...lowEntropyTestData,
...result,
})
}),
toJSON: jest.fn(() => {
return lowEntropyTestData
}),
}

options = { apiKey: 'foo' }
analytics = new Analytics({ writeKey: options.apiKey })
Expand Down Expand Up @@ -199,14 +171,6 @@ describe('Segment.io', () => {
assert(body.traits == null)
assert(body.timestamp)
})

it('should add userAgentData when available', async () => {
await analytics.track('event')
const [_, params] = spyMock.mock.calls[0]
const body = JSON.parse(params.body)

expect(body.context?.userAgentData).toEqual(lowEntropyTestData)
})
})

describe('#group', () => {
Expand Down
19 changes: 2 additions & 17 deletions packages/browser/src/plugins/segmentio/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import standard, { StandardDispatcherConfig } from './fetch-dispatcher'
import { normalize } from './normalize'
import { scheduleFlush } from './schedule-flush'
import { SEGMENT_API_HOST } from '../../core/constants'
import { clientHints } from '../../lib/client-hints'
import { UADataValues } from '../../lib/client-hints/interfaces'

type DeliveryStrategy =
| {
Expand Down Expand Up @@ -52,11 +50,11 @@ function onAlias(analytics: Analytics, json: JSON): JSON {
return json
}

export async function segmentio(
export function segmentio(
analytics: Analytics,
settings?: SegmentioSettings,
integrations?: LegacySettings['integrations']
): Promise<Plugin> {
): Plugin {
// Attach `pagehide` before buffer is created so that inflight events are added
// to the buffer before the buffer persists events in its own `pagehide` handler.
window.addEventListener('pagehide', () => {
Expand Down Expand Up @@ -86,15 +84,6 @@ export async function segmentio(
? batch(apiHost, deliveryStrategy.config)
: standard(deliveryStrategy?.config as StandardDispatcherConfig)

let userAgentData: UADataValues | undefined
try {
userAgentData = await clientHints(
analytics.options.highEntropyValuesClientHints
)
} catch {
userAgentData = undefined
}

async function send(ctx: Context): Promise<Context> {
if (isOffline()) {
buffer.push(ctx)
Expand All @@ -107,10 +96,6 @@ export async function segmentio(

const path = ctx.event.type.charAt(0)

if (userAgentData && ctx.event.context) {
ctx.event.context.userAgentData = userAgentData
}

let json = toFacade(ctx.event).json()

if (ctx.event.type === 'track') {
Expand Down
1 change: 1 addition & 0 deletions packages/config-webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"@types/circular-dependency-plugin": "^5",
"babel-loader": "^8.0.0",
"circular-dependency-plugin": "^5.2.2",
"ecma-version-validator-webpack-plugin": "^1.2.1",
"terser-webpack-plugin": "^5.1.4",
"webpack": "^5.76.0",
"webpack-cli": "^4.8.0",
Expand Down
6 changes: 5 additions & 1 deletion packages/config-webpack/webpack.config.common.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const TerserPlugin = require('terser-webpack-plugin')
const CircularDependencyPlugin = require('circular-dependency-plugin')
const {
ECMAVersionValidatorPlugin,
} = require('ecma-version-validator-webpack-plugin')

const isProd = process.env.NODE_ENV === 'production'
const isWatch = process.env.WATCH === 'true'
Expand All @@ -15,6 +18,7 @@ module.exports = {
devtool: 'source-map',
stats: isWatch ? 'errors-warnings' : 'normal',
mode: isProd ? 'production' : 'development',
target: ['web', 'es5'], // target es5 for ie11 support (generates module boilerplate in es5)
module: {
rules: [
{
Expand All @@ -37,7 +41,6 @@ module.exports = {
},
},
],
exclude: /node_modules/,
},
],
},
Expand Down Expand Up @@ -67,5 +70,6 @@ module.exports = {
new CircularDependencyPlugin({
failOnError: true,
}),
new ECMAVersionValidatorPlugin({ ecmaVersion: 5 }), // ensure our js bundle only contains syntax supported in ie11. This does not check polyfills.
],
}
2 changes: 1 addition & 1 deletion packages/consent/consent-tools-integration-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ Why is this using wd.io instead of playwright?
## Development
### Build, start server, run tests (and exit gracefully)
```
yarn . test:intg
yarn . test:int
```
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"private": true,
"scripts": {
".": "yarn run -T turbo run --filter=@internal/consent-tools-integration-tests...",
"test:intg": "yarn wdio:local",
"test:int": "yarn wdio:local",
"build": "webpack",
"watch": "yarn build --watch",
"wdio:local": "wdio wdio.conf.local.ts",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { AnalyticsBrowser } from '@segment/analytics-next'
import { createWrapper } from '@segment/analytics-consent-tools'

const fakeCategories = { Advertising: true, Analytics: true }
const fakeCategories = { FooCategory1: true, FooCategory2: true }

const withCMP = createWrapper({
getCategories: () => fakeCategories,
integrationCategoryMappings: {
Fullstory: ['Analytics'],
'Actions Amplitude': ['Advertising'],
},
})

const analytics = new AnalyticsBrowser()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ export abstract class BasePage {
{
creationName: 'FullStory',
consentSettings: {
categories: ['Analytics'],
categories: ['FooCategory2'],
},
},
{
creationName: 'Actions Amplitude',
consentSettings: {
categories: ['Advertising'],
categories: ['FooCategory1'],
},
}
)
Expand Down
Loading

0 comments on commit 8391e3a

Please sign in to comment.