-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: mixpanel type 설정 및 identify 유틸 생성 * chore: 불필요 주석 삭제 * chore: 불필요 주석 삭제 * feat: 로그인 시 mixpanel identify 설정 * test: mixpanel 유틸
- Loading branch information
Showing
9 changed files
with
96 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { mixpanelTrack, setMixpanelIdentify } from './index'; | ||
|
||
describe('libs/mixpanel/index', () => { | ||
it('mixpanelTrack이 정의되어 있어야 함', () => { | ||
expect(mixpanelTrack).toBeDefined(); | ||
}); | ||
|
||
it('setMixpanelIdentify이 정의되어 있어야 함', () => { | ||
expect(setMixpanelIdentify).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { mixpanelTrack } from './mixpanel'; | ||
export { mixpanelTrack, setMixpanelIdentify } from './mixpanel'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { mixpanelTrack, setMixpanelIdentify } from './mixpanel'; | ||
|
||
describe('libs/mixpanel/mixpanel', () => { | ||
const mixpanelTrackSpy = jest.fn(); | ||
const mixpanelIdentifySpy = jest.fn(); | ||
|
||
const setWindowMixpanelWithSpy = () => { | ||
Object.assign(window, { mixpanel: { track: mixpanelTrackSpy, identify: mixpanelIdentifySpy } }); | ||
}; | ||
|
||
const setWindowMixpanelToUndefined = () => { | ||
Object.assign(window, { mixpanel: undefined }); | ||
}; | ||
|
||
describe('window에 mixpanel이 없을 시', () => { | ||
beforeEach(() => { | ||
setWindowMixpanelToUndefined(); | ||
}); | ||
|
||
it('window.mixpanel.track이 호출되지 않음', () => { | ||
mixpanelTrack('test', { test: 'test' }); | ||
expect(mixpanelTrackSpy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('window.mixpanel.identify가 호출되지 않음', () => { | ||
setMixpanelIdentify('test'); | ||
expect(mixpanelIdentifySpy).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('window에 mixpanel이 있을 시', () => { | ||
beforeEach(() => { | ||
setWindowMixpanelWithSpy(); | ||
}); | ||
|
||
it('window.mixpanel.track이 호출됨', () => { | ||
setWindowMixpanelWithSpy(); | ||
mixpanelTrack('test', { test: 'test' }); | ||
expect(mixpanelTrackSpy).toHaveBeenCalled(); | ||
expect(mixpanelTrackSpy).toHaveBeenCalledWith('test', [{ test: 'test' }]); | ||
}); | ||
|
||
it('mixpanel.identify가 호출됨', () => { | ||
setMixpanelIdentify('test'); | ||
expect(mixpanelIdentifySpy).toHaveBeenCalled(); | ||
expect(mixpanelIdentifySpy).toHaveBeenCalledWith('test'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,21 @@ | ||
export function mixpanelTrack(event_name: string, ...props: any) { | ||
function callWhenMixpanelReady(callback: VoidFunction) { | ||
try { | ||
if ((window as any).mixpanel) { | ||
(window as any).mixpanel.track(event_name, props); | ||
if (window?.mixpanel) { | ||
callback(); | ||
} | ||
} catch (e) { | ||
console.log(e); | ||
console.error(e); | ||
} | ||
} | ||
|
||
export function mixpanelTrack(event_name: string, ...props: any) { | ||
callWhenMixpanelReady(() => { | ||
window.mixpanel.track(event_name, props); | ||
}); | ||
} | ||
|
||
export function setMixpanelIdentify(userId: string) { | ||
callWhenMixpanelReady(() => { | ||
window.mixpanel.identify(userId); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters