Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky committed Sep 22, 2023
1 parent 519fae6 commit e7e9a4f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ describe('Pre-initialization', () => {
})

await sleep(100) // the snippet does not return a promise (pre-initialization) ... it sometimes has a callback as the third argument.
expect(trackSpy).toBeCalledWith('foo')
expect(trackSpy).toBeCalledWith('bar')
expect(trackSpy).toBeCalledWith('foo', bufferedPageCtxFixture)
expect(trackSpy).toBeCalledWith('bar', bufferedPageCtxFixture)
expect(trackSpy).toBeCalledTimes(2)

expect(identifySpy).toBeCalledTimes(1)
Expand Down
4 changes: 2 additions & 2 deletions packages/browser/src/core/buffer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Context } from '../context'
import { isThenable } from '../../lib/is-thenable'
import { AnalyticsBrowserCore } from '../analytics/interfaces'
import { version } from '../../generated/version'

import { getGlobalAnalytics } from '../../lib/global-analytics-helper'
import {
isBufferedPageContext,
BufferedPageContext,
Expand Down Expand Up @@ -191,7 +191,7 @@ export class PreInitMethodCallBuffer {
* This removes existing buffered calls from the window object.
*/
private _pushSnippetWindowBuffer(): void {
const wa = window.analytics
const wa = getGlobalAnalytics()
if (!Array.isArray(wa)) return undefined
const buffered: SnippetBuffer = wa.splice(0, wa.length)
const calls = buffered.map((v) => this._transformSnippetCall(v))
Expand Down
15 changes: 8 additions & 7 deletions packages/browser/src/core/page/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
getDefaultBufferedPageContext,
getDefaultPageContext,
isBufferedPageContext,
BUFFERED_PAGE_CONTEXT_DISCRIMINANT_KEY,
} from '../'

const ogLocation = window.location
Expand All @@ -17,19 +16,21 @@ beforeEach(() => {

describe(isBufferedPageContext, () => {
it('should return true if object is page context', () => {
expect(isBufferedPageContext(getDefaultBufferedPageContext())).toBe(true)
})
it('should returm false if object is not page context', () => {
expect(isBufferedPageContext(undefined)).toBe(false)
expect(isBufferedPageContext({})).toBe(false)
expect(isBufferedPageContext('')).toBe(false)
expect(isBufferedPageContext({ foo: false })).toBe(false)
expect(isBufferedPageContext({ u: 'hello' })).toBe(false)
expect(isBufferedPageContext(null)).toBe(false)
expect(
isBufferedPageContext({
__type: BUFFERED_PAGE_CONTEXT_DISCRIMINANT_KEY,
foo: 123,
...getDefaultBufferedPageContext(),
some_unknown_key: 123,
})
).toBe(false)

expect(isBufferedPageContext(getDefaultBufferedPageContext())).toBe(true)

expect(isBufferedPageContext(null)).toBe(false)
})
})

Expand Down
79 changes: 45 additions & 34 deletions packages/browser/src/core/page/get-page-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,53 @@ export interface PageContext {
url: string
}

export const BUFFERED_PAGE_CONTEXT_DISCRIMINANT_KEY = 'buffered_page_ctx'
type CanonicalURL = string | undefined

/* This is */
export interface BufferedPageContext {
__type: typeof BUFFERED_PAGE_CONTEXT_DISCRIMINANT_KEY
pathname: string
href: string
search: string
title: string
referrer: string
canonicalUrl: string | undefined
c: CanonicalURL
p: PageContext['path']
u: PageContext['url']
s: PageContext['search']
t: PageContext['title']
r: PageContext['referrer']
}

export const createBufferedPageContext = (
href: string,
canonicalUrl: string | undefined,
url: string,
canonicalUrl: CanonicalURL,
search: string,
pathname: string,
path: string,
title: string,
referrer: string
): BufferedPageContext => {
return {
__type: BUFFERED_PAGE_CONTEXT_DISCRIMINANT_KEY,
search: search,
canonicalUrl,
href,
pathname,
title,
referrer: referrer,
c: canonicalUrl,
p: path,
u: url,
s: search,
t: title,
r: referrer,
}
}

export function isBufferedPageContext(v: unknown): v is BufferedPageContext {
return (
isPlainObject(v) &&
'__type' in v &&
v['__type'] === BUFFERED_PAGE_CONTEXT_DISCRIMINANT_KEY &&
'href' in v &&
typeof v['href'] === 'string'
)
export function isBufferedPageContext(
bufferedPageCtx: unknown
): bufferedPageCtx is BufferedPageContext {
if (!isPlainObject(bufferedPageCtx)) return false

// my clever/dubious way of making sure this type guard does not get out sync with the type definition
const expectedKeys = Object.keys(
createBufferedPageContext('', '', '', '', '', '')
) as (keyof BufferedPageContext)[]

// if object has both the correct key names AND the right number of keys, assume it's a BufferedPageContext
const bufferedPageCtxKeys = Object.keys(bufferedPageCtx)
if (bufferedPageCtxKeys.length !== expectedKeys.length) return false
for (const k of expectedKeys) {
if (!(k in bufferedPageCtx)) return false
}
return true
}

// Legacy logic: we are we appending search parameters to the canonical URL -- I guess the canonical URL is "not canonical enough" (lol)
Expand All @@ -70,19 +79,21 @@ const formatCanonicalPath = (canonicalUrl: string) => {
return a.pathname[0] === '/' ? a.pathname : '/' + a.pathname
}

export const createPageContext = (u: BufferedPageContext): PageContext => {
const path = u.canonicalUrl ? formatCanonicalPath(u.canonicalUrl) : u.pathname
const url = u.canonicalUrl
? createCanonicalURL(u.canonicalUrl, u.search)
: removeHash(u.href)
export const createPageContext = (
buffered: BufferedPageContext
): PageContext => {
const path = buffered.c ? formatCanonicalPath(buffered.c) : buffered.p
const url = buffered.c
? createCanonicalURL(buffered.c, buffered.s)
: removeHash(buffered.u)

// Why are we removing the anchor here but not the canonical URL? Also, why don't we include hash or any anchor arguments. (???)
// There's no way for a customer to get access to hash arguments without overriding url =S
return {
path,
referrer: u.referrer,
search: u.search,
title: u.title,
referrer: buffered.r,
search: buffered.s,
title: buffered.t,
url,
}
}
Expand Down

0 comments on commit e7e9a4f

Please sign in to comment.