Skip to content

Commit

Permalink
fix: track Params to allow eventProps as third param
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhi591 authored and rohitesh-wingify committed Jul 25, 2024
1 parent dae28bc commit e916e39
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 20 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.4.0] - 2024-06-21

### Fixed

- fix: add support for DSL where featureIdValue could be `off`
- refactor: make eventProperties as third parameter

## [1.3.0] - 2024-06-20

### Fixed
Expand Down
10 changes: 5 additions & 5 deletions lib/VWOClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export interface IVWOClient {
getFlag(featureKey: string, context: Record<string, any>): Record<any, any>;
trackEvent(
eventName: string,
eventProperties: Record<string, dynamic>,
context: Record<string, any>,
eventProperties: Record<string, dynamic>,
): Promise<Record<string, boolean>>;
setAttribute(attributeKey: string, attributeValue: string, context: Record<string, any>): void;
}
Expand Down Expand Up @@ -151,14 +151,14 @@ export class VWOClient implements IVWOClient {
* This method validates the types of the inputs and ensures the settings and user context are valid before proceeding.
*
* @param {string} eventName - The name of the event to track.
* @param {Record<string, dynamic>} eventProperties - The properties associated with the event.
* @param {ContextModel} context - The context in which the event is being tracked, must include a valid user ID.
* @param {Record<string, dynamic>} eventProperties - The properties associated with the event.
* @returns {Promise<Record<string, boolean>>} - A promise that resolves to the result of the tracking operation.
*/
trackEvent(
eventName: string,
eventProperties: Record<string, dynamic> = {},
context: Record<string, any>,
eventProperties: Record<string, dynamic> = {},
): Promise<Record<string, boolean>> {
const apiName = 'trackEvent';
const deferredObject = new Deferred();
Expand Down Expand Up @@ -192,7 +192,7 @@ export class VWOClient implements IVWOClient {
LogManager.Instance.error(
buildMessage(ErrorLogMessagesEnum.API_INVALID_PARAM, {
apiName,
key: 'featureKey',
key: 'eventProperties',
type: getType(eventProperties),
correctType: 'object',
}),
Expand All @@ -217,7 +217,7 @@ export class VWOClient implements IVWOClient {

// Proceed with tracking the event
new TrackApi()
.track(this.settings, eventName, eventProperties, contextModel, hookManager)
.track(this.settings, eventName, contextModel, eventProperties, hookManager)
.then((data) => {
deferredObject.resolve(data);
})
Expand Down
6 changes: 3 additions & 3 deletions lib/api/TrackEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ interface ITrack {
* Tracks an event with given properties and context.
* @param settings Configuration settings for the tracking.
* @param eventName Name of the event to track.
* @param eventProperties Properties associated with the event.
* @param context Contextual information like user details.
* @param eventProperties Properties associated with the event.
* @param hookManager Manager for handling hooks and callbacks.
* @returns A promise that resolves to a record indicating the success or failure of the event tracking.
*/
track(
settings: SettingsModel,
eventName: string,
eventProperties: any,
context: ContextModel,
eventProperties: any,
hookManager: HooksManager,
): Promise<Record<string, boolean>>;
}
Expand All @@ -50,8 +50,8 @@ export class TrackApi implements ITrack {
async track(
settings: SettingsModel,
eventName: string,
eventProperties: any,
context: ContextModel,
eventProperties: any,
hookManager: HooksManager,
): Promise<Record<string, boolean>> {
if (doesEventBelongToAnyFeature(eventName, settings)) {
Expand Down
18 changes: 13 additions & 5 deletions lib/packages/segmentation-evaluator/evaluators/SegmentEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,20 @@ export class SegmentEvaluator implements Segmentation {
const featureIdKey: string = Object.keys(featureIdObject)[0];
const featureIdValue: string = featureIdObject[featureIdKey];

if (featureIdValue === 'on') {
if (featureIdValue === 'on' || featureIdValue === 'off') {
const features = this.settings.getFeatures();
const feature = features.find((feature) => feature.getId() === parseInt(featureIdKey));

if (feature) {
const featureKey = feature.getKey();
const result = await this.checkInUserStorage(this.settings, featureKey, this.context);
// if the result is false, then we need to return true as feature is not present in the user storage
if (featureIdValue === 'off') {
return !result;
}
return result;
} else {
console.error('Feature not found with featureIdKey:', featureIdKey);
LogManager.Instance.error('Feature not found with featureIdKey: ' + featureIdKey);
return null; // Handle the case when feature is not found
}
}
Expand Down Expand Up @@ -197,7 +201,7 @@ export class SegmentEvaluator implements Segmentation {
async checkLocationPreSegmentation(locationMap: Record<string, dynamic>): Promise<boolean> {
// Ensure user's IP address is available
if (this.context?.getIpAddress() === undefined) {
LogManager.Instance.info('To evaluate location pre Segment, please pass ipAddress in context object');
LogManager.Instance.error('To evaluate location pre Segment, please pass ipAddress in context object');
return false;
}
// Check if location data is available and matches the expected values
Expand All @@ -219,7 +223,7 @@ export class SegmentEvaluator implements Segmentation {
async checkUserAgentParser(uaParserMap: Record<string, string[]>): Promise<boolean> {
// Ensure user's user agent is available
if (!this.context?.getUserAgent() || this.context?.getUserAgent() === undefined) {
LogManager.Instance.info('To evaluate user agent related segments, please pass userAgent in context object');
LogManager.Instance.error('To evaluate user agent related segments, please pass userAgent in context object');
return false;
}
// Check if user agent data is available and matches the expected values
Expand Down Expand Up @@ -264,6 +268,10 @@ export class SegmentEvaluator implements Segmentation {
for (const key in actualMap) {
if (Object.prototype.hasOwnProperty.call(expectedMap, key)) {
const expectedValues = expectedMap[key];
// convert expected values to lowercase
expectedValues.forEach((value, index) => {
expectedValues[index] = value.toLowerCase();
});
const actualValue = actualMap[key];

// Handle wildcard patterns for all keys
Expand All @@ -273,7 +281,7 @@ export class SegmentEvaluator implements Segmentation {
// Extract pattern from wildcard string
const wildcardPattern = val.slice(9, -1);
// Convert wildcard pattern to regex and check if it matches the actual value
const regex = new RegExp(wildcardPattern.replace(/\*/g, '.*')); // Convert wildcard pattern to regex
const regex = new RegExp(wildcardPattern.replace(/\*/g, '.*'), 'i'); // Convert wildcard pattern to regex, 'i' for case-insensitive
// Check if the actual value matches the regex pattern for the key
if (regex.test(actualValue)) {
// match found, return true as we only need to check if any of the expected values match the actual value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class SegmentOperandEvaluator {
const listIdRegex = /inlist\((\w+:\d+)\)/;
const match = operand.match(listIdRegex);
if (!match || match.length < 2) {
console.error("Invalid 'inList' operand format");
LogManager.Instance.error("Invalid 'inList' operand format");
return false;
}

Expand All @@ -74,7 +74,7 @@ export class SegmentOperandEvaluator {
}
return res;
} catch (error) {
console.error('Error while fetching data:', error);
LogManager.Instance.error('Error while fetching data: ' + error);
return false;
}
} else {
Expand Down
10 changes: 5 additions & 5 deletions test/e2e/TrackEvent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('VWOClient trackEvent method', () => {
const context = { id: '123' };

// Call the trackEvent method
const result = await vwoClient.trackEvent(eventName, eventProperties, context);
const result = await vwoClient.trackEvent(eventName, context, eventProperties);

// Assert that the method resolves with the correct data
expect(result).toEqual({ [eventName]: true });
Expand All @@ -59,7 +59,7 @@ describe('VWOClient trackEvent method', () => {
const context = { id: '123' };

// Call the trackEvent method
const result = await vwoClient.trackEvent(eventName, eventProperties, context);
const result = await vwoClient.trackEvent(eventName, context, eventProperties);

// Assert that the method resolves with the correct data
expect(result).toEqual({ [eventName]: false });
Expand All @@ -74,7 +74,7 @@ describe('VWOClient trackEvent method', () => {
const context = { id: '123' };

// Call the trackEvent method
const result = await vwoClient.trackEvent(eventName, eventProperties, context);
const result = await vwoClient.trackEvent(eventName, context, eventProperties);

// Assert that the method resolves with the correct data
expect(result).toEqual({ [eventName]: false });
Expand All @@ -89,7 +89,7 @@ describe('VWOClient trackEvent method', () => {
const context = { id: '123' };

// Call the trackEvent method
const result = await vwoClient.trackEvent(eventName, eventProperties, context);
const result = await vwoClient.trackEvent(eventName, context, eventProperties);

// Assert that the method resolves with the correct data
expect(result).toEqual({ [eventName]: false });
Expand All @@ -104,7 +104,7 @@ describe('VWOClient trackEvent method', () => {
const context = {}; // Invalid context without userId

// Call the trackEvent method
const result = await vwoClient.trackEvent(eventName, eventProperties, context);
const result = await vwoClient.trackEvent(eventName, context, eventProperties);

// Assert that the method resolves with the correct data
expect(result).toEqual({ [eventName]: false });
Expand Down

0 comments on commit e916e39

Please sign in to comment.