-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add initTrackPageview and trackPageview APIs
- Loading branch information
1 parent
c6ec0fe
commit 1652eda
Showing
13 changed files
with
228 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { init } from './init.ts'; | ||
export { init } from './init'; | ||
export { initTrackPageview } from './trackPageView'; |
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,74 @@ | ||
/** | ||
* This entire file will eventually be moved to the React open-analytics package | ||
*/ | ||
|
||
import { RouteComponentProps } from 'react-router'; | ||
import { markNTBT } from './utils/perfume'; | ||
import { PAGEVIEW_EVENT_TYPE } from './utils/constants'; | ||
import { trackEvent } from './trackEvent'; | ||
import { ActionType, ComponentType } from './types/perfume'; | ||
import { getLocation, getConfig } from './storage/storage'; | ||
import { setPageviewConfig } from './storage/location'; | ||
|
||
/** | ||
* Public method to manually track a pageview | ||
*/ | ||
|
||
type LogPageViewOptions = { | ||
callMarkNTBT?: boolean; | ||
}; | ||
|
||
type RouterHistory = RouteComponentProps['history']; | ||
|
||
type TrackPageviewOptions = { | ||
blacklistRegex?: RegExp[]; | ||
browserHistory: RouterHistory; | ||
}; | ||
|
||
export const getUrlPathname = (): string => { | ||
return window?.location?.pathname || ''; | ||
}; | ||
|
||
export function trackPageView( | ||
options: LogPageViewOptions = { callMarkNTBT: true } | ||
) { | ||
const config = getConfig(); | ||
const { | ||
pageviewConfig: { blacklistRegex }, | ||
} = getLocation(); | ||
|
||
// Stop log if platform is not initialized | ||
if (config.platform === 'unknown') { | ||
return; | ||
} | ||
|
||
// Avoid pageview for blacklist pathnames | ||
if (blacklistRegex.some((r: RegExp) => r.test(getUrlPathname()))) { | ||
return; | ||
} | ||
trackEvent({ | ||
name: PAGEVIEW_EVENT_TYPE, | ||
action: ActionType.render, | ||
component: ComponentType.page, | ||
}); | ||
if (options.callMarkNTBT) { | ||
markNTBT(); | ||
} | ||
} | ||
|
||
/** | ||
* Initiate auto instrumentation for the `pageview` events. | ||
*/ | ||
export const initTrackPageview = ({ | ||
blacklistRegex, | ||
browserHistory, | ||
}: TrackPageviewOptions) => { | ||
setPageviewConfig({ | ||
blacklistRegex: blacklistRegex || [], | ||
isEnabled: true, | ||
}); | ||
trackPageView({ callMarkNTBT: false }); | ||
browserHistory.listen(() => { | ||
trackPageView(); | ||
}); | ||
}; |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export const PAGEVIEW_EVENT_TYPE = 'pageview'; | ||
|
||
type PerformanceEvents = Record< | ||
string, | ||
{ | ||
|
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.