vwo-fme-react-sdk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/CHANGELOG.md +80 -0
  2. package/LICENSE +202 -0
  3. package/NOTICE +18 -0
  4. package/README.md +352 -0
  5. package/dist/VWOContext.d.ts +24 -0
  6. package/dist/VWOProvider.d.ts +35 -0
  7. package/dist/index.d.ts +23 -0
  8. package/dist/index.js +8 -0
  9. package/dist/logger/LogMessageBuilder.d.ts +63 -0
  10. package/dist/logger/Logger.d.ts +46 -0
  11. package/dist/logger/core/LogManager.d.ts +100 -0
  12. package/dist/logger/core/TransportManager.d.ts +80 -0
  13. package/dist/logger/enums/LogLevelEnum.d.ts +22 -0
  14. package/dist/logger/index.d.ts +17 -0
  15. package/dist/logger/transports/ConsoleTransport.d.ts +60 -0
  16. package/dist/services/loggerService.d.ts +35 -0
  17. package/dist/types/Common.d.ts +36 -0
  18. package/dist/useGetFlag.d.ts +21 -0
  19. package/dist/useGetFlagVariable.d.ts +29 -0
  20. package/dist/useSetAttribute.d.ts +20 -0
  21. package/dist/useTrackEvent.d.ts +21 -0
  22. package/dist/useVWOClient.d.ts +20 -0
  23. package/dist/utils/DataTypeUtil.d.ts +38 -0
  24. package/dist/vwo-fme-react-sdk.cjs.development.js +904 -0
  25. package/dist/vwo-fme-react-sdk.cjs.development.js.map +1 -0
  26. package/dist/vwo-fme-react-sdk.cjs.production.min.js +904 -0
  27. package/dist/vwo-fme-react-sdk.cjs.production.min.js.map +1 -0
  28. package/dist/vwo-fme-react-sdk.esm.js +886 -0
  29. package/dist/vwo-fme-react-sdk.esm.js.map +1 -0
  30. package/lib/VWOContext.ts +47 -0
  31. package/lib/VWOProvider.tsx +103 -0
  32. package/lib/index.ts +29 -0
  33. package/lib/logger/LogMessageBuilder.ts +99 -0
  34. package/lib/logger/Logger.ts +51 -0
  35. package/lib/logger/core/LogManager.ts +164 -0
  36. package/lib/logger/core/TransportManager.ts +142 -0
  37. package/lib/logger/enums/LogLevelEnum.ts +23 -0
  38. package/lib/logger/index.ts +18 -0
  39. package/lib/logger/transports/ConsoleTransport.ts +85 -0
  40. package/lib/services/LoggerService.ts +49 -0
  41. package/lib/types/Common.ts +42 -0
  42. package/lib/useGetFlag.ts +81 -0
  43. package/lib/useGetFlagVariable.ts +65 -0
  44. package/lib/useSetAttribute.ts +48 -0
  45. package/lib/useTrackEvent.ts +52 -0
  46. package/lib/useVWOClient.ts +41 -0
  47. package/lib/utils/DataTypeUtil.ts +48 -0
  48. package/package.json +84 -0
@@ -0,0 +1,142 @@
1
+ /**
2
+ * Copyright 2025 Wingify Software Pvt. Ltd.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import { dynamic } from '../../types/Common';
18
+ import { LogLevelEnum } from '../enums/LogLevelEnum';
19
+ import { LogMessageBuilder } from '../LogMessageBuilder';
20
+ import { Logger } from '../Logger';
21
+ import { isFunction } from '../../utils/DataTypeUtil';
22
+
23
+ enum LogLevelNumberEnum {
24
+ TRACE = 0,
25
+ DEBUG = 1,
26
+ INFO = 2,
27
+ WARN = 3,
28
+ ERROR = 4,
29
+ }
30
+
31
+ interface IlogTransport extends Logger {
32
+ transports: Array<Record<string, dynamic>>;
33
+ config: Record<string, dynamic>;
34
+
35
+ // Determines if a message should be logged based on the transport and configuration levels
36
+ shouldLog(transportLevel: string, configLevel: string): boolean;
37
+ // Logs a message at a specified level
38
+ log(level: string, message: string): void;
39
+ }
40
+
41
+ /**
42
+ * Manages logging transports and delegates logging messages to them based on configuration.
43
+ * Implements the IlogTransport interface.
44
+ */
45
+ export class LogTransportManager implements IlogTransport {
46
+ transports: Array<Record<string, any>>;
47
+ config: Record<string, any>;
48
+
49
+ /**
50
+ * Initializes the manager with a configuration object.
51
+ * @param {Record<string, any>} config - Configuration settings for the log manager.
52
+ */
53
+ constructor(config: Record<string, any>) {
54
+ this.transports = [];
55
+ this.config = config;
56
+ }
57
+
58
+ /**
59
+ * Adds a new transport to the manager.
60
+ * @param {Record<string, any>} transport - The transport object to be added.
61
+ */
62
+ addTransport(transport: Record<string, any>): void {
63
+ this.transports.push(transport);
64
+ }
65
+
66
+ /**
67
+ * Determines if the log should be processed based on the transport and configuration levels.
68
+ * @param {string} transportLevel - The log level set for the transport.
69
+ * @param {string} configLevel - The log level set in the configuration.
70
+ * @returns {boolean} - Returns true if the log level is appropriate for logging, false otherwise.
71
+ */
72
+ shouldLog(transportLevel: string, configLevel: string): boolean {
73
+ // Default to the most specific level available
74
+ // transportLevel = transportLevel || configLevel || this.config.level;
75
+
76
+ const targetLevel = LogLevelNumberEnum[transportLevel.toUpperCase()];
77
+ const desiredLevel = LogLevelNumberEnum[(configLevel || this.config.level).toUpperCase()];
78
+
79
+ return targetLevel >= desiredLevel;
80
+ }
81
+
82
+ /**
83
+ * Logs a message at TRACE level.
84
+ * @param {string} message - The message to log.
85
+ */
86
+ trace(message: string): void {
87
+ this.log(LogLevelEnum.TRACE, message);
88
+ }
89
+
90
+ /**
91
+ * Logs a message at DEBUG level.
92
+ * @param {string} message - The message to log.
93
+ */
94
+ debug(message: string): void {
95
+ this.log(LogLevelEnum.DEBUG, message);
96
+ }
97
+
98
+ /**
99
+ * Logs a message at INFO level.
100
+ * @param {string} message - The message to log.
101
+ */
102
+ info(message: string): void {
103
+ this.log(LogLevelEnum.INFO, message);
104
+ }
105
+
106
+ /**
107
+ * Logs a message at WARN level.
108
+ * @param {string} message - The message to log.
109
+ */
110
+ warn(message: string): void {
111
+ this.log(LogLevelEnum.WARN, message);
112
+ }
113
+
114
+ /**
115
+ * Logs a message at ERROR level.
116
+ * @param {string} message - The message to log.
117
+ */
118
+ error(message: string): void {
119
+ this.log(LogLevelEnum.ERROR, message);
120
+ }
121
+
122
+ /**
123
+ * Delegates the logging of messages to the appropriate transports.
124
+ * @param {string} level - The level at which to log the message.
125
+ * @param {string} message - The message to log.
126
+ */
127
+ log(level: string, message: string): void {
128
+ for (let i = 0; i < this.transports.length; i++) {
129
+ const logMessageBuilder = new LogMessageBuilder(this.config, this.transports[i]);
130
+ const formattedMessage = logMessageBuilder.formatMessage(level, message);
131
+ if (this.shouldLog(level, this.transports[i].level)) {
132
+ if (this.transports[i].log && isFunction(this.transports[i].log)) {
133
+ // Use custom log handler if available
134
+ this.transports[i].log(level, message);
135
+ } else {
136
+ // Otherwise, use the default log method
137
+ this.transports[i][level](formattedMessage);
138
+ }
139
+ }
140
+ }
141
+ }
142
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Copyright 2025 Wingify Software Pvt. Ltd.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ export enum LogLevelEnum {
18
+ TRACE = 'trace',
19
+ DEBUG = 'debug',
20
+ INFO = 'info',
21
+ WARN = 'warn',
22
+ ERROR = 'error',
23
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Copyright 2025 Wingify Software Pvt. Ltd.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ export { LogManager, ILogManager } from './core/LogManager';
18
+ export { LogLevelEnum } from './enums/LogLevelEnum';
@@ -0,0 +1,85 @@
1
+ /**
2
+ * Copyright 2025 Wingify Software Pvt. Ltd.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import { LogLevelEnum } from '../enums/LogLevelEnum';
18
+ import { Logger } from '../Logger';
19
+
20
+ /**
21
+ * ConsoleTransport class implements the Logger interface to provide logging functionality.
22
+ * It outputs logs to the console based on the log level set in the configuration.
23
+ */
24
+ export class ConsoleTransport implements Logger {
25
+ config: Record<string, any>; // Configuration object for the logger
26
+ level: string; // Current log level
27
+
28
+ /**
29
+ * Constructor initializes the ConsoleTransport with a configuration object.
30
+ * @param {Record<string, any>} config - Configuration settings for the logger, including 'level'.
31
+ */
32
+ constructor(config: Record<string, any> = {}) {
33
+ this.config = config; // Store the configuration
34
+ this.level = this.config.level; // Set the logging level from the configuration
35
+ }
36
+
37
+ /**
38
+ * Logs a trace message.
39
+ * @param {string} message - The message to log.
40
+ */
41
+ trace(message: string): void {
42
+ this.consoleLog(LogLevelEnum.TRACE, message);
43
+ }
44
+
45
+ /**
46
+ * Logs a debug message.
47
+ * @param {string} message - The message to log.
48
+ */
49
+ debug(message: string): void {
50
+ this.consoleLog(LogLevelEnum.DEBUG, message);
51
+ }
52
+
53
+ /**
54
+ * Logs an informational message.
55
+ * @param {string} message - The message to log.
56
+ */
57
+ info(message: string): void {
58
+ this.consoleLog(LogLevelEnum.INFO, message);
59
+ }
60
+
61
+ /**
62
+ * Logs a warning message.
63
+ * @param {string} message - The message to log.
64
+ */
65
+ warn(message: string): void {
66
+ this.consoleLog(LogLevelEnum.WARN, message);
67
+ }
68
+
69
+ /**
70
+ * Logs an error message.
71
+ * @param {string} message - The message to log.
72
+ */
73
+ error(message: string): void {
74
+ this.consoleLog(LogLevelEnum.ERROR, message);
75
+ }
76
+
77
+ /**
78
+ * Generic log function that logs messages to the console based on the log level.
79
+ * @param {string} level - The log level under which the message should be logged.
80
+ * @param {string} message - The message to log.
81
+ */
82
+ consoleLog(level: string, message: string): void {
83
+ console[level](message); // Use console's logging function dynamically based on the level
84
+ }
85
+ }
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Copyright 2025 Wingify Software Pvt. Ltd.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ /**
18
+ * logger.ts - Global Logger Singleton
19
+ */
20
+ import { LogManager } from '../logger/core/LogManager';
21
+
22
+ let logger: LogManager | null = null;
23
+
24
+ /**
25
+ * Initializes the global logger instance.
26
+ * This function should be called inside `VWOProvider` before logging is used anywhere in the application.
27
+ *
28
+ * @param {object} config - The logger configuration object.
29
+ * @param {object} config.logger - Optional logging configuration (e.g., log level, transports).
30
+ * @returns {void} No return value; initializes the logger instance.
31
+ */
32
+ export function initLogger(config: any) {
33
+ if (!logger) {
34
+ logger = new LogManager(config.logger || {});
35
+ }
36
+ }
37
+
38
+ /**
39
+ * Retrieves the global logger instance.
40
+ * Ensures that `initLogger` has been called before attempting to use logging.
41
+ *
42
+ * @returns {LogManager} The global logger instance.
43
+ */
44
+ export function getLogger(): LogManager {
45
+ if (!logger) {
46
+ logger = new LogManager({ level: 'error' });
47
+ }
48
+ return logger;
49
+ }
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Copyright 2025 Wingify Software Pvt. Ltd.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ /**
18
+ * Defines a type `dynamic` which can be used to represent a variety of data types.
19
+ * This type is flexible and can handle multiple types including primitive types,
20
+ * complex objects, and collections.
21
+ */
22
+ export type dynamic =
23
+ | boolean // Represents a boolean value
24
+ | number // Represents a numeric value
25
+ | string // Represents a string value
26
+ | Date // Represents a date object
27
+ | void // Represents the absence of a value
28
+ | undefined // Represents an undefined value
29
+ | null // Represents a null value
30
+ | Record<string, any> // Represents an object with string keys and any type of values
31
+ | Array<dynamicArray> // Represents an array of `dynamicArray` type
32
+ | Map<string, dynamicArray>; // Represents a map with string keys and `dynamicArray` type values
33
+
34
+ /**
35
+ * Defines a type `dynamicArray` which is used within the `dynamic` type.
36
+ * This type is intended for use in arrays and supports several basic data types and objects.
37
+ */
38
+ export type dynamicArray =
39
+ | boolean // Represents a boolean value
40
+ | number // Represents a numeric value
41
+ | string // Represents a string value
42
+ | Record<string, any>; // Represents an object with string keys and any type of values
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Copyright 2025 Wingify Software Pvt. Ltd.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import { useState, useEffect, useMemo } from 'react';
18
+ import { useVWOContext } from './VWOContext';
19
+ import { getLogger } from './services/LoggerService';
20
+ import { isObject } from './utils/DataTypeUtil';
21
+ import { dynamic } from './types/Common';
22
+
23
+ /**
24
+ * Hook to get a feature flag value
25
+ * @param featureKey - The key of the feature flag to get
26
+ * @returns The feature flag value
27
+ */
28
+ export const useGetFlag = (featureKey: string) => {
29
+ // This is the return schema for the flag if the feature key is not found or the flag is not enabled
30
+ const errorReturnSchema = {
31
+ isEnabled: (): boolean => false,
32
+ getVariables: (): Array<Record<string, dynamic>> => [],
33
+ getVariable: (_key: string, defaultValue: any): dynamic => defaultValue,
34
+ };
35
+ const logger = getLogger();
36
+ try {
37
+ if (!featureKey) {
38
+ logger.error('Feature key is required for useGetFlag hook');
39
+ return errorReturnSchema;
40
+ }
41
+
42
+ const [flag, setFlag] = useState<any | null>(null);
43
+ const { vwoClient, userContext } = useVWOContext();
44
+
45
+ if (!vwoClient) {
46
+ logger.error('VWO Client is missing in useGetFlag hook. Ensure VWOProvider is correctly initialized.');
47
+ return errorReturnSchema;
48
+ }
49
+
50
+ if (!userContext || !isObject(userContext)) {
51
+ logger.error('Invalid user context in useGetFlag hook. Ensure a valid userContext is provided.');
52
+ return errorReturnSchema;
53
+ }
54
+
55
+ // Memoize the userContext to avoid unnecessary re-fetching
56
+ const stableUserContext = useMemo(() => userContext, [userContext]);
57
+
58
+ // Memoize the getFlag function to avoid re-creation
59
+ const getFlag = useMemo(() => {
60
+ return async () => {
61
+ try {
62
+ const result = await vwoClient.getFlag(featureKey, stableUserContext);
63
+ setFlag(result);
64
+ } catch (error) {
65
+ logger.error(`Error fetching feature flag "${featureKey}": ${error}`);
66
+ setFlag({});
67
+ }
68
+ };
69
+ }, [featureKey, vwoClient, stableUserContext]);
70
+
71
+ // Runs only when `getFlag` reference changes
72
+ useEffect(() => {
73
+ getFlag();
74
+ }, [getFlag]);
75
+
76
+ return flag;
77
+ } catch (error) {
78
+ logger.error(`Error getting feature flag: ${error}`);
79
+ return errorReturnSchema;
80
+ }
81
+ };
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Copyright 2025 Wingify Software Pvt. Ltd.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import { isObject } from './utils/DataTypeUtil';
18
+ import { getLogger } from './services/LoggerService';
19
+
20
+ /**
21
+ * Hook to get all variables from a flag
22
+ * @param flag - The flag to get the variables from
23
+ * @returns The variables from the flag
24
+ */
25
+ export const useGetFlagVariables = (flag: any) => {
26
+ const logger = getLogger();
27
+ try {
28
+ if (!flag || !isObject(flag)) {
29
+ logger.error('Flag is required for useGetFlagVariables hook and should be an object');
30
+ return [];
31
+ }
32
+
33
+ return flag.getVariables();
34
+ } catch (error) {
35
+ logger.error(`Error getting flag variables: ${error}`);
36
+ return [];
37
+ }
38
+ };
39
+
40
+ /**
41
+ * Hook to get a flag variable
42
+ * @param flag - The flag to get the variable from
43
+ * @param variableKey - The key of the variable to get
44
+ * @param defaultValue - The default value to return if the variable is not found
45
+ * @returns The value of the variable
46
+ */
47
+ export const useGetFlagVariable = (flag: any, variableKey: string, defaultValue: any) => {
48
+ const logger = getLogger();
49
+ try {
50
+ if (!flag || !isObject(flag)) {
51
+ logger.error('Flag is required for useGetFlagVariable hook and should be an object');
52
+ return [];
53
+ }
54
+
55
+ if (!flag || !variableKey) {
56
+ logger.error('Flag and variable key are required for useGetFlagVariable hook');
57
+ return defaultValue;
58
+ }
59
+
60
+ return flag.getVariable(variableKey, defaultValue);
61
+ } catch (error) {
62
+ logger.error(`Error getting flag variable: ${error}`);
63
+ return defaultValue;
64
+ }
65
+ };
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Copyright 2025 Wingify Software Pvt. Ltd.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import { useVWOContext } from './VWOContext';
18
+ import { isObject } from './utils/DataTypeUtil';
19
+ import { getLogger } from './services/LoggerService';
20
+
21
+ /**
22
+ * Hook to set attributes for the user
23
+ * @param attributeMap - The map of attributes to set
24
+ */
25
+ export const useSetAttribute = (attributeMap: Record<string, string>) => {
26
+ const logger = getLogger();
27
+ try {
28
+ if (!attributeMap || !isObject(attributeMap) || Object.keys(attributeMap).length === 0) {
29
+ logger.error(
30
+ 'attributeMap(object having key-value pairs of user attributes) is required for useSetAttribute hook',
31
+ );
32
+ return;
33
+ }
34
+
35
+ // Fetch the vwoClient and userContext from the context
36
+ const { vwoClient, userContext } = useVWOContext();
37
+
38
+ if (!userContext || !isObject(userContext)) {
39
+ logger.error('Invalid user context in useSetAttribute hook. Ensure a valid userContext is provided.');
40
+ return {};
41
+ }
42
+
43
+ // Set the attributes
44
+ vwoClient.setAttribute(attributeMap, userContext);
45
+ } catch (error) {
46
+ logger.error(`Error setting attributes: ${error}`);
47
+ }
48
+ };
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Copyright 2025 Wingify Software Pvt. Ltd.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import { getLogger } from './services/LoggerService';
18
+ import { useVWOContext } from './VWOContext';
19
+ import { isObject, isString } from './utils/DataTypeUtil';
20
+
21
+ /**
22
+ * Hook to track an event
23
+ * @param eventName - The name of the event to track
24
+ * @param eventProperties - The properties of the event to track (optional)
25
+ */
26
+ export const useTrackEvent = (eventName: string, eventProperties: Record<string, string> = {}) => {
27
+ const logger = getLogger();
28
+ try {
29
+ if (!eventName && !isString(eventName)) {
30
+ logger.error('Event name is required for useTrackEvent hook and it should be a string');
31
+ return;
32
+ }
33
+
34
+ // Fetch the vwoClient and userContext from the context
35
+ const { vwoClient, userContext } = useVWOContext();
36
+
37
+ if (!vwoClient) {
38
+ logger.error('VWO Client is missing in useTrackEvent hook. Ensure VWOProvider is correctly initialized.');
39
+ return {};
40
+ }
41
+
42
+ if (!userContext || !isObject(userContext)) {
43
+ logger.error('Invalid user context in useTrackEvent hook. Ensure a valid userContext is provided.');
44
+ return {};
45
+ }
46
+
47
+ // Track the event
48
+ vwoClient.trackEvent(eventName, userContext, eventProperties);
49
+ } catch (error) {
50
+ logger.error(`Error tracking event: ${error}`);
51
+ }
52
+ };
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Copyright 2025 Wingify Software Pvt. Ltd.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import { getLogger } from './services/LoggerService';
18
+ import { useVWOContext } from './VWOContext';
19
+
20
+ /**
21
+ * Returns the VWO SDK client instance
22
+ * @returns VWO SDK client instance
23
+ */
24
+ export const useVWOClient = () => {
25
+ const logger = getLogger();
26
+ try {
27
+ const context = useVWOContext();
28
+
29
+ // If the VWO SDK client is not found, throw an error
30
+ if (!context || !context.vwoClient) {
31
+ logger.error('useVWOClient must be used within a VWOProvider !!');
32
+ return null;
33
+ }
34
+
35
+ // Return the VWO SDK client
36
+ return context.vwoClient;
37
+ } catch (error) {
38
+ logger.error(`Error in useVWOClient hook: ${error}`);
39
+ return null;
40
+ }
41
+ };