Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@
"default": null,
"depends_on": null
},
"sourceName": {
"label": "Source Name",
"description": "This value, if it's not blank, will be sent as segment_source_name to Mixpanel for every event, including events Mixpanel captures automatically.",
"type": "string",
"required": false,
"multiple": false,
"choices": null,
"default": null,
"depends_on": null
},
"api_host": {
"label": "API Host",
"description": "The Mixpanel API host to send data to.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ describe('Mixpanel Web initialization', () => {
track: jest.fn(),
track_pageview: jest.fn(),
identify: jest.fn(),
register: jest.fn(),
alias: jest.fn(),
get_group: jest.fn(),
set_group: jest.fn(),
Expand Down Expand Up @@ -99,4 +100,136 @@ describe('Mixpanel Web initialization', () => {
expect(resolvedInstance).toBe(mockMixpanelInstance)
expect(resolvedInstance).not.toBe(mockSnippetMixpanel)
})

test('registers segment_source_name as a super property when sourceName is set', async () => {
const mockMixpanelInstance: Mixpanel = {
init: jest.fn(),
track: jest.fn(),
track_pageview: jest.fn(),
identify: jest.fn(),
register: jest.fn(),
alias: jest.fn(),
get_group: jest.fn(),
set_group: jest.fn(),
people: {
set: jest.fn(),
set_once: jest.fn(),
increment: jest.fn()
}
}

const mockSnippetMixpanel: Partial<Mixpanel> = {
init: jest.fn().mockImplementation((_token, config, _name) => {
setTimeout(() => {
if (config?.loaded) {
config.loaded(mockMixpanelInstance)
}
}, 10)
})
}

jest.spyOn(initScriptModule, 'initScript').mockImplementation(async () => {
;(window as any).mixpanel = mockSnippetMixpanel
})

const [event] = await MixpanelDestination({
...baseSettings,
sourceName: 'My Website Source',
subscriptions
})

await event.load(Context.system(), {} as Analytics)

expect(mockMixpanelInstance.register).toHaveBeenCalledWith({
segment_source_name: 'My Website Source'
})

// sourceName is a Segment side setting and is not a valid Mixpanel config option,
// so it must not be forwarded to mixpanel.init
const [, config] = (mockSnippetMixpanel.init as jest.Mock).mock.calls[0]
expect(config).not.toHaveProperty('sourceName')
})

test('does not register a super property when sourceName is not set', async () => {
const mockMixpanelInstance: Mixpanel = {
init: jest.fn(),
track: jest.fn(),
track_pageview: jest.fn(),
identify: jest.fn(),
register: jest.fn(),
alias: jest.fn(),
get_group: jest.fn(),
set_group: jest.fn(),
people: {
set: jest.fn(),
set_once: jest.fn(),
increment: jest.fn()
}
}

const mockSnippetMixpanel: Partial<Mixpanel> = {
init: jest.fn().mockImplementation((_token, config, _name) => {
setTimeout(() => {
if (config?.loaded) {
config.loaded(mockMixpanelInstance)
}
}, 10)
})
}

jest.spyOn(initScriptModule, 'initScript').mockImplementation(async () => {
;(window as any).mixpanel = mockSnippetMixpanel
})

const [event] = await MixpanelDestination({
...baseSettings,
subscriptions
})

await event.load(Context.system(), {} as Analytics)

expect(mockMixpanelInstance.register).not.toHaveBeenCalled()
})

test('does not register a super property when sourceName is only whitespace', async () => {
const mockMixpanelInstance: Mixpanel = {
init: jest.fn(),
track: jest.fn(),
track_pageview: jest.fn(),
identify: jest.fn(),
register: jest.fn(),
alias: jest.fn(),
get_group: jest.fn(),
set_group: jest.fn(),
people: {
set: jest.fn(),
set_once: jest.fn(),
increment: jest.fn()
}
}

const mockSnippetMixpanel: Partial<Mixpanel> = {
init: jest.fn().mockImplementation((_token, config, _name) => {
setTimeout(() => {
if (config?.loaded) {
config.loaded(mockMixpanelInstance)
}
}, 10)
})
}

jest.spyOn(initScriptModule, 'initScript').mockImplementation(async () => {
;(window as any).mixpanel = mockSnippetMixpanel
})

const [event] = await MixpanelDestination({
...baseSettings,
sourceName: ' ',
subscriptions
})

await event.load(Context.system(), {} as Analytics)

expect(mockMixpanelInstance.register).not.toHaveBeenCalled()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ describe('Mixpanel.alias', () => {
track_pageview: jest.fn(),
track: jest.fn(),
identify: jest.fn(),
register: jest.fn(),
alias: jest.fn(),
get_group: jest.fn().mockReturnValue(mockGroup),
set_group: jest.fn(),
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ describe('Mixpanel.group', () => {
track_pageview: jest.fn(),
track: jest.fn(),
identify: jest.fn(),
register: jest.fn(),
alias: jest.fn(),
get_group: jest.fn().mockReturnValue(mockGroup),
set_group: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ describe('Mixpanel.identify', () => {
track_pageview: jest.fn(),
track: jest.fn(),
identify: jest.fn(),
register: jest.fn(),
alias: jest.fn(),
get_group: jest.fn().mockReturnValue(mockGroup),
set_group: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const destination: BrowserDestinationDefinition<Settings, Mixpanel> = {
const {
projectToken,
name,
sourceName,
autocapture,
pageview,
click,
Expand Down Expand Up @@ -88,7 +89,14 @@ export const destination: BrowserDestinationDefinition<Settings, Mixpanel> = {
}

return new Promise<Mixpanel>((resolve) => {
config.loaded = (mp) => resolve(mp)
config.loaded = (mp) => {
const trimmedSourceName = sourceName?.trim()
if (trimmedSourceName && typeof mp?.register === 'function') {
// Registered as a super property so it is attached to every event
mp.register({ segment_source_name: trimmedSourceName })
}
resolve(mp)
}
Comment on lines +92 to +99

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be more defensive?
Something like the following:

config.loaded = (mp) => {
  const trimmed = sourceName?.trim?.()
  if (typeof mp?.register === 'function' && trimmed) {
    mp.register({ segment_source_name: trimmed })
  }
  
  resolve(mp)
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call on the defensive check, pushing an update now and adding a related test.


if (name) {
window.mixpanel.init(projectToken, config, name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ export const settingFields: Record<string, GlobalSetting> = {
description: 'The name for the new mixpanel instance that you want created.',
type: 'string'
},
sourceName: {
label: 'Source Name',
description:
"This value, if it's not blank, will be sent as segment_source_name to Mixpanel for every event, including events Mixpanel captures automatically.",
type: 'string'
},
api_host: {
description: 'The Mixpanel API host to send data to.',
label: 'API Host',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ describe('Mixpanel.track', () => {
track_pageview: jest.fn(),
track: jest.fn(),
identify: jest.fn(),
register: jest.fn(),
alias: jest.fn(),
get_group: jest.fn().mockReturnValue(mockGroup),
set_group: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ describe('Mixpanel.trackPageView', () => {
track_pageview: jest.fn(),
track: jest.fn(),
identify: jest.fn(),
register: jest.fn(),
alias: jest.fn(),
get_group: jest.fn().mockReturnValue(mockGroup),
set_group: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export interface Mixpanel {

identify(unique_id?: string): void

register(properties: { [k: string]: unknown }): void

alias(alias: string, original?: string): void

get_group(group_key: string, group_id: string): Group
Expand Down
Loading