-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathaction.ts
More file actions
131 lines (114 loc) · 3.75 KB
/
action.ts
File metadata and controls
131 lines (114 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { RNConfig } from "@/lib/common/config";
import { Logger } from "@/lib/common/logger";
import { shouldDisplayBasedOnPercentage } from "@/lib/common/utils";
import { SurveyStore } from "@/lib/survey/store";
import type { TSurvey } from "@/types/survey";
import {
type InvalidCodeError,
type NetworkError,
type Result,
err,
okVoid,
} from "@/types/error";
import { fetch } from "@react-native-community/netinfo";
/**
* Triggers the display of a survey if it meets the display percentage criteria
* @param survey - The survey configuration to potentially display
*/
export const triggerSurvey = (survey: TSurvey): void => {
const surveyStore = SurveyStore.getInstance();
const logger = Logger.getInstance();
// Check if the survey should be displayed based on displayPercentage
if (survey.displayPercentage) {
const shouldDisplaySurvey = shouldDisplayBasedOnPercentage(
survey.displayPercentage
);
if (!shouldDisplaySurvey) {
logger.debug(
`Survey display of "${survey.name}" skipped based on displayPercentage.`
);
return; // skip displaying the survey
}
}
surveyStore.setSurvey(survey);
};
/**
* Tracks an action name and triggers associated surveys
* @param name - The name of the action to track
* @param alias - Optional alias for the action name
* @returns Result indicating success or network error
*/
export const trackAction = async (
name: string,
alias?: string
): Promise<Result<void, NetworkError>> => {
const logger = Logger.getInstance();
const appConfig = await RNConfig.getInstance();
const aliasName = alias ?? name;
logger.debug(`Formbricks: Action "${aliasName}" tracked`);
// get a list of surveys that are collecting insights
const activeSurveys = appConfig.get().filteredSurveys;
if (Boolean(activeSurveys) && activeSurveys.length > 0) {
for (const survey of activeSurveys) {
for (const trigger of survey.triggers) {
if (trigger.actionClass.name === name) {
triggerSurvey(survey);
}
}
}
} else {
logger.debug("No active surveys to display");
}
return okVoid();
};
/**
* Tracks an action by its code and triggers associated surveys (used for code actions only)
* @param code - The action code to track
* @returns Result indicating success, network error, or invalid code error
*/
export const track = async (
code: string
): Promise<
| Result<void, NetworkError>
| Result<void, InvalidCodeError>
| Result<void, { code: "error"; message: string }>
> => {
try {
const appConfig = await RNConfig.getInstance();
const netInfo = await fetch();
if (!netInfo.isConnected) {
return err({
code: "network_error",
status: 500,
message:
"No internet connection. Please check your connection and try again.",
responseMessage:
"No internet connection. Please check your connection and try again.",
url: new URL(`${appConfig.get().appUrl}/js/surveys.umd.cjs`),
});
}
const {
environment: {
data: { actionClasses = [] },
},
} = appConfig.get();
const codeActionClasses = actionClasses.filter(
(action) => action.type === "code"
);
const actionClass = codeActionClasses.find((action) => action.key === code);
if (!actionClass) {
return err({
code: "invalid_code",
message: `Action with identifier '${code}' is unknown. Please add this action in Formbricks in order to use it via the SDK action tracking.`,
});
}
return trackAction(actionClass.name, code);
} catch (error) {
const logger = Logger.getInstance();
logger.error(`Error tracking action ${error as string}`);
return err({
code: "error",
message: "Error tracking action",
});
}
};