Skip to content

Commit 2a0523b

Browse files
committed
feat: Convert event_tag_utils module to TS (#544)
* Convert event_tag_utils module to TS * Convert event_tag_utils module to TS * Add type guard to avoid using any * Move 'if (eventTags)' to top level functions * Move let variables to inside the functions
1 parent 0b9fd11 commit 2a0523b

File tree

5 files changed

+103
-79
lines changed

5 files changed

+103
-79
lines changed

packages/optimizely-sdk/lib/core/event_builder/event_helpers.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { getLogger } from '@optimizely/js-sdk-logging';
1717

1818
import fns from '../../utils/fns';
1919
import projectConfig from '../project_config';
20-
import eventTagUtils from '../../utils/event_tag_utils';
20+
import * as eventTagUtils from '../../utils/event_tag_utils';
2121
import * as attributesValidator from'../../utils/attributes_validator';
2222

2323
var logger = getLogger('EVENT_BUILDER');
@@ -106,6 +106,14 @@ export var buildConversionEvent = function(config) {
106106
var eventTags = config.eventTags;
107107
var eventId = projectConfig.getEventId(configObj, eventKey);
108108

109+
let revenue = null;
110+
let eventValue = null;
111+
112+
if (eventTags) {
113+
revenue = eventTagUtils.getRevenueValue(eventTags, logger);
114+
eventValue = eventTagUtils.getEventValue(eventTags, logger);
115+
}
116+
109117
return {
110118
type: 'conversion',
111119
timestamp: fns.currentTimestamp(),
@@ -131,8 +139,8 @@ export var buildConversionEvent = function(config) {
131139
key: eventKey,
132140
},
133141

134-
revenue: eventTagUtils.getRevenueValue(eventTags, logger),
135-
value: eventTagUtils.getEventValue(eventTags, logger),
142+
revenue: revenue,
143+
value: eventValue,
136144
tags: eventTags,
137145
};
138146
};

packages/optimizely-sdk/lib/core/event_builder/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import fns from '../../utils/fns';
1717
import enums from '../../utils/enums';
1818
import projectConfig from '../project_config';
19-
import eventTagUtils from '../../utils/event_tag_utils';
19+
import * as eventTagUtils from '../../utils/event_tag_utils';
2020
import * as attributeValidator from '../../utils/attributes_validator';
2121

2222
var ACTIVATE_EVENT_KEY = 'campaign_activated';

packages/optimizely-sdk/lib/utils/event_tag_utils/index.js

Lines changed: 0 additions & 74 deletions
This file was deleted.

packages/optimizely-sdk/lib/utils/event_tag_utils/index.tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import sinon from 'sinon';
1717
import { assert } from 'chai';
1818

19-
import eventTagUtils from './';
19+
import * as eventTagUtils from './';
2020

2121
describe('lib/utils/event_tag_utils', function() {
2222
var mockLogger;
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* Copyright 2017, 2019-2020 Optimizely
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+
import { sprintf } from '@optimizely/js-sdk-utils';
17+
18+
import { EventTags } from '@optimizely/js-sdk-event-processor';
19+
import { LoggerFacade } from '@optimizely/js-sdk-logging';
20+
21+
import {
22+
LOG_LEVEL,
23+
LOG_MESSAGES,
24+
RESERVED_EVENT_KEYWORDS,
25+
} from '../enums';
26+
27+
/**
28+
* Provides utility method for parsing event tag values
29+
*/
30+
const MODULE_NAME = 'EVENT_TAG_UTILS';
31+
const REVENUE_EVENT_METRIC_NAME = RESERVED_EVENT_KEYWORDS.REVENUE;
32+
const VALUE_EVENT_METRIC_NAME = RESERVED_EVENT_KEYWORDS.VALUE;
33+
34+
/**
35+
* Grab the revenue value from the event tags. "revenue" is a reserved keyword.
36+
* @param {EventTags} eventTags
37+
* @param {LoggerFacade} logger
38+
* @return {number|null}
39+
*/
40+
export function getRevenueValue(eventTags: EventTags, logger: LoggerFacade): number | null {
41+
if (eventTags.hasOwnProperty(REVENUE_EVENT_METRIC_NAME)) {
42+
const rawValue = eventTags[REVENUE_EVENT_METRIC_NAME];
43+
let parsedRevenueValue;
44+
if (typeof rawValue === 'string') {
45+
parsedRevenueValue = parseInt(rawValue);
46+
if (isNaN(parsedRevenueValue)) {
47+
logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.FAILED_TO_PARSE_REVENUE, MODULE_NAME, rawValue));
48+
return null;
49+
}
50+
logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.PARSED_REVENUE_VALUE, MODULE_NAME, parsedRevenueValue));
51+
return parsedRevenueValue;
52+
}
53+
if (typeof rawValue === 'number') {
54+
parsedRevenueValue = rawValue;
55+
logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.PARSED_REVENUE_VALUE, MODULE_NAME, parsedRevenueValue));
56+
return parsedRevenueValue;
57+
}
58+
return null;
59+
}
60+
return null;
61+
}
62+
63+
/**
64+
* Grab the event value from the event tags. "value" is a reserved keyword.
65+
* @param {EventTags} eventTags
66+
* @param {LoggerFacade} logger
67+
* @return {number|null}
68+
*/
69+
export function getEventValue(eventTags: EventTags, logger: LoggerFacade): number | null {
70+
if (eventTags.hasOwnProperty(VALUE_EVENT_METRIC_NAME)) {
71+
const rawValue = eventTags[VALUE_EVENT_METRIC_NAME];
72+
let parsedEventValue;
73+
if (typeof rawValue === 'string') {
74+
parsedEventValue = parseFloat(rawValue);
75+
if (isNaN(parsedEventValue)) {
76+
logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.FAILED_TO_PARSE_VALUE, MODULE_NAME, rawValue));
77+
return null;
78+
}
79+
logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.PARSED_NUMERIC_VALUE, MODULE_NAME, parsedEventValue));
80+
return parsedEventValue;
81+
}
82+
if (typeof rawValue === 'number') {
83+
parsedEventValue = rawValue;
84+
logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.PARSED_NUMERIC_VALUE, MODULE_NAME, parsedEventValue));
85+
return parsedEventValue;
86+
}
87+
return null;
88+
}
89+
return null;
90+
}

0 commit comments

Comments
 (0)