forked from microsoft/applicationinsights-react-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove dependecy on applicationinsights-react-js & add EngagementTracker
- Loading branch information
Kinga Marek
committed
Dec 11, 2023
1 parent
2537dc2
commit ad288cd
Showing
6 changed files
with
1,755 additions
and
1,536 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
43 changes: 43 additions & 0 deletions
43
sample/applicationinsights-react-sample/src/EngagementTracker.tsx
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,43 @@ | ||
import React, { useEffect } from "react"; | ||
import { appInsights } from "./ApplicationInsightsService"; | ||
|
||
type EngagementTrackerProps = { | ||
readonly name: string; | ||
readonly children: React.ReactNode; | ||
}; | ||
|
||
export function EngagementTracker({ name, children }: EngagementTrackerProps) { | ||
const displayedTimestamp = React.useRef<number | null>(null); | ||
useEffect(() => { | ||
document.addEventListener('visibilitychange', handleVisibilityChange); | ||
displayedTimestamp.current = Date.now(); | ||
return () => { | ||
document.removeEventListener('visibilitychange', handleVisibilityChange); | ||
if (displayedTimestamp.current !== null) { | ||
sendMetric("unrendered"); | ||
|
||
} | ||
}; | ||
}, []); | ||
const handleVisibilityChange = () => { | ||
if (document.visibilityState === 'visible' && displayedTimestamp.current === null) { | ||
displayedTimestamp.current = Date.now(); | ||
} else if (document.visibilityState === 'hidden') { | ||
sendMetric("hidden"); | ||
} | ||
}; | ||
const sendMetric = (displayFinishedCause: string) => { | ||
if (displayedTimestamp.current !== null) { | ||
const displayDuration = Date.now() - displayedTimestamp.current; | ||
displayedTimestamp.current = null; | ||
if (displayDuration > 0) { | ||
appInsights.trackMetric({ | ||
name: `${name} display duration (s)`, | ||
average: displayDuration / 1000, | ||
properties: { displayFinishedCause: displayFinishedCause } | ||
}); | ||
} | ||
} | ||
} | ||
return <>{children}</>; | ||
} |
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
Oops, something went wrong.