Skip to content

Commit 799ffd5

Browse files
[Hubspot] Add sanitizeValue function to Trim Whitespace and Remove Invisible Characters (#812)
* Sanitize values - Add function to sanitize values - Add function to trim whitespaces and remove invisible characters - Add unit test cases * update hubspot version in package json --------- Co-authored-by: Arijit Ray <35370469+itsarijitray@users.noreply.github.com>
1 parent 46f7202 commit 799ffd5

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

integrations/hubspot/lib/index.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,45 @@ HubSpot.prototype.loaded = function() {
7070
return libLoaded && !!(window.hbspt && window.hbspt.forms);
7171
};
7272

73+
/**
74+
* Trims whitespace and invisible characters from a string.
75+
*
76+
* @param {string} str - The string to trim.
77+
* @returns {string} - The trimmed string.
78+
*/
79+
function trimWhitespaceAndInvisibleChars(str) {
80+
// This regular expression matches leading and trailing whitespace characters,
81+
// as well as invisible characters such as zero-width spaces and non-breaking spaces.
82+
return str.replace(
83+
/^[\s\u200B-\u200D\uFEFF]+|[\s\u200B-\u200D\uFEFF]+$/g,
84+
''
85+
);
86+
}
87+
88+
/**
89+
* Recursively sanitizes the values of an object or array by trimming whitespace
90+
* and invisible characters from string values, and preserving the structure of
91+
* functions, arrays, and nested objects.
92+
*
93+
* @param {Object|Array} msg - The object or array to sanitize.
94+
* @returns {Object|Array} - The sanitized object or array.
95+
*/
96+
function sanitizeValue(msg) {
97+
if (typeof msg === 'object' && msg !== null) {
98+
Object.keys(msg).forEach(function(key) {
99+
var value = msg[key];
100+
if (typeof value === 'string') {
101+
msg[key] = trimWhitespaceAndInvisibleChars(value);
102+
} else if (Array.isArray(value)) {
103+
msg[key] = value.map(sanitizeValue);
104+
} else if (typeof value === 'object') {
105+
msg[key] = sanitizeValue(value);
106+
}
107+
});
108+
}
109+
return msg;
110+
}
111+
73112
/**
74113
* Page.
75114
*
@@ -89,6 +128,7 @@ HubSpot.prototype.page = function() {
89128
*/
90129

91130
HubSpot.prototype.identify = function(identify) {
131+
sanitizeValue(identify);
92132
// use newer version of Identify to have access to `companyName`
93133
var newIdentify = new Identify({
94134
traits: identify.traits(),
@@ -121,6 +161,7 @@ HubSpot.prototype.identify = function(identify) {
121161
*/
122162

123163
HubSpot.prototype.track = function(track) {
164+
sanitizeValue(track);
124165
// Hubspot expects properties.id to be the name of the .track() event
125166
// Ref: http://developers.hubspot.com/docs/methods/enterprise_events/javascript_api
126167
var props = convertDates(track.properties({ id: '_id', revenue: 'value' }));

integrations/hubspot/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@segment/analytics.js-integration-hubspot",
33
"description": "The Hubspot analytics.js integration.",
4-
"version": "2.2.4",
4+
"version": "2.2.5",
55
"keywords": [
66
"analytics.js",
77
"analytics.js-integration",

integrations/hubspot/test/index.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,22 @@ describe('HubSpot', function() {
251251
}
252252
]);
253253
});
254+
255+
it('should sanitize values in identify method', function() {
256+
analytics.identify({
257+
email: ' name@example.com ',
258+
company: {
259+
name: ' Example Company '
260+
}
261+
});
262+
analytics.called(window._hsq.push, [
263+
'identify',
264+
{
265+
email: 'name@example.com',
266+
company: 'Example Company'
267+
}
268+
]);
269+
});
254270
});
255271

256272
describe('#track', function() {
@@ -314,6 +330,18 @@ describe('HubSpot', function() {
314330
{ id: 'Did Something Valuable', _id: '12345', value: 13 }
315331
]);
316332
});
333+
334+
it('should sanitize values in track method', function() {
335+
analytics.track(' Did Something Valuable ', {
336+
id: ' 12345 ',
337+
revenue: 13
338+
});
339+
analytics.called(window._hsq.push, [
340+
'trackEvent',
341+
'Did Something Valuable',
342+
{ id: 'Did Something Valuable', _id: '12345', value: 13 }
343+
]);
344+
});
317345
});
318346

319347
describe('#page', function() {

0 commit comments

Comments
 (0)