Skip to content

Commit

Permalink
fix: Exit parent function on shouldSkipTracking() (#2656)
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr authored Jan 12, 2024
1 parent ada5fe3 commit b7e2717
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions editor.planx.uk/src/pages/FlowEditor/lib/analyticsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
const previousBreadcrumbs = usePrevious(breadcrumbs);

const trackVisibilityChange = () => {
skipUpdateIfNotTracking();
if (shouldSkipTracking()) return;

switch (document.visibilityState) {
case "hidden":
send(
Expand Down Expand Up @@ -285,14 +286,13 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
});
}

function skipUpdateIfNotTracking() {
if (!shouldTrackAnalytics || !lastVisibleNodeAnalyticsLogId) {
return;
}
function shouldSkipTracking() {
return (!shouldTrackAnalytics || !lastVisibleNodeAnalyticsLogId)
}

async function trackHelpClick(metadata?: HelpClickMetadata) {
skipUpdateIfNotTracking();
if (shouldSkipTracking()) return;

await publicClient.mutate({
mutation: gql`
mutation UpdateHasClickedHelp($id: bigint!, $metadata: jsonb = {}) {
Expand All @@ -313,7 +313,8 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
}

async function trackNextStepsLinkClick(metadata?: SelectedUrlsMetadata) {
skipUpdateIfNotTracking();
if (shouldSkipTracking()) return;

await publicClient.mutate({
mutation: gql`
mutation UpdateHasClickNextStepsLink(
Expand All @@ -338,7 +339,8 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
async function trackFlowDirectionChange(
flowDirection: AnalyticsLogDirection,
) {
skipUpdateIfNotTracking();
if (shouldSkipTracking()) return;

await publicClient.mutate({
mutation: gql`
mutation UpdateFlowDirection($id: bigint!, $flow_direction: String) {
Expand All @@ -361,7 +363,8 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
initiator: BackwardsNavigationInitiatorType,
nodeId?: string,
) {
skipUpdateIfNotTracking();
if (shouldSkipTracking()) return;

const targetNodeMetadata = nodeId ? getTargetNodeDataFromFlow(nodeId) : {};
const metadata: Record<string, NodeMetadata> = {};
metadata[`${initiator}`] = targetNodeMetadata;
Expand Down Expand Up @@ -426,7 +429,8 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
}

async function updateLastVisibleNodeLogWithAllowListAnswers(nodeId: string) {
skipUpdateIfNotTracking();
if (shouldSkipTracking()) return;


const allowListAnswers = getAllowListAnswers(nodeId);
if (!allowListAnswers) return;
Expand Down Expand Up @@ -505,7 +509,8 @@ export const AnalyticsProvider: React.FC<{ children: React.ReactNode }> = ({
* Capture user input errors caught by ErrorWrapper component
*/
async function trackInputErrors(error: string) {
skipUpdateIfNotTracking();
if (shouldSkipTracking()) return;

await publicClient.mutate({
mutation: gql`
mutation TrackInputErrors($id: bigint!, $error: jsonb) {
Expand Down

0 comments on commit b7e2717

Please sign in to comment.