Skip to content

feat: Convert event_tag_utils module to TS #544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions packages/optimizely-sdk/lib/core/event_builder/event_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { getLogger } from '@optimizely/js-sdk-logging';

import fns from '../../utils/fns';
import projectConfig from '../project_config';
import eventTagUtils from '../../utils/event_tag_utils';
import * as eventTagUtils from '../../utils/event_tag_utils';
import * as attributesValidator from'../../utils/attributes_validator';

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

let revenue = null;
let eventValue = null;

if (eventTags) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mjc1283 FYI if (eventTags) {...} already exists in event_builder/index.js, so this is the only place where we need to put it.

revenue = eventTagUtils.getRevenueValue(eventTags, logger);
eventValue = eventTagUtils.getEventValue(eventTags, logger);
}

return {
type: 'conversion',
timestamp: fns.currentTimestamp(),
Expand All @@ -131,8 +139,8 @@ export var buildConversionEvent = function(config) {
key: eventKey,
},

revenue: eventTagUtils.getRevenueValue(eventTags, logger),
value: eventTagUtils.getEventValue(eventTags, logger),
revenue: revenue,
value: eventValue,
tags: eventTags,
};
};
Expand Down
2 changes: 1 addition & 1 deletion packages/optimizely-sdk/lib/core/event_builder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import fns from '../../utils/fns';
import enums from '../../utils/enums';
import projectConfig from '../project_config';
import eventTagUtils from '../../utils/event_tag_utils';
import * as eventTagUtils from '../../utils/event_tag_utils';
import * as attributeValidator from '../../utils/attributes_validator';

var ACTIVATE_EVENT_KEY = 'campaign_activated';
Expand Down
74 changes: 0 additions & 74 deletions packages/optimizely-sdk/lib/utils/event_tag_utils/index.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import sinon from 'sinon';
import { assert } from 'chai';

import eventTagUtils from './';
import * as eventTagUtils from './';

describe('lib/utils/event_tag_utils', function() {
var mockLogger;
Expand Down
90 changes: 90 additions & 0 deletions packages/optimizely-sdk/lib/utils/event_tag_utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* Copyright 2017, 2019-2020 Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { sprintf } from '@optimizely/js-sdk-utils';

import { EventTags } from '@optimizely/js-sdk-event-processor';
import { LoggerFacade } from '@optimizely/js-sdk-logging';

import {
LOG_LEVEL,
LOG_MESSAGES,
RESERVED_EVENT_KEYWORDS,
} from '../enums';

/**
* Provides utility method for parsing event tag values
*/
const MODULE_NAME = 'EVENT_TAG_UTILS';
const REVENUE_EVENT_METRIC_NAME = RESERVED_EVENT_KEYWORDS.REVENUE;
const VALUE_EVENT_METRIC_NAME = RESERVED_EVENT_KEYWORDS.VALUE;

/**
* Grab the revenue value from the event tags. "revenue" is a reserved keyword.
* @param {EventTags} eventTags
* @param {LoggerFacade} logger
* @return {number|null}
*/
export function getRevenueValue(eventTags: EventTags, logger: LoggerFacade): number | null {
if (eventTags.hasOwnProperty(REVENUE_EVENT_METRIC_NAME)) {
const rawValue = eventTags[REVENUE_EVENT_METRIC_NAME];
let parsedRevenueValue;
if (typeof rawValue === 'string') {
parsedRevenueValue = parseInt(rawValue);
if (isNaN(parsedRevenueValue)) {
logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.FAILED_TO_PARSE_REVENUE, MODULE_NAME, rawValue));
return null;
}
logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.PARSED_REVENUE_VALUE, MODULE_NAME, parsedRevenueValue));
return parsedRevenueValue;
}
if (typeof rawValue === 'number') {
parsedRevenueValue = rawValue;
logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.PARSED_REVENUE_VALUE, MODULE_NAME, parsedRevenueValue));
return parsedRevenueValue;
}
return null;
}
return null;
}

/**
* Grab the event value from the event tags. "value" is a reserved keyword.
* @param {EventTags} eventTags
* @param {LoggerFacade} logger
* @return {number|null}
*/
export function getEventValue(eventTags: EventTags, logger: LoggerFacade): number | null {
if (eventTags.hasOwnProperty(VALUE_EVENT_METRIC_NAME)) {
const rawValue = eventTags[VALUE_EVENT_METRIC_NAME];
let parsedEventValue;
if (typeof rawValue === 'string') {
parsedEventValue = parseFloat(rawValue);
if (isNaN(parsedEventValue)) {
logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.FAILED_TO_PARSE_VALUE, MODULE_NAME, rawValue));
return null;
}
logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.PARSED_NUMERIC_VALUE, MODULE_NAME, parsedEventValue));
return parsedEventValue;
}
if (typeof rawValue === 'number') {
parsedEventValue = rawValue;
logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.PARSED_NUMERIC_VALUE, MODULE_NAME, parsedEventValue));
return parsedEventValue;
}
return null;
}
return null;
}