Skip to content

Commit aed4a42

Browse files
feat: user profile interface
1 parent d725146 commit aed4a42

1 file changed

Lines changed: 141 additions & 19 deletions

File tree

lib/countly.js

Lines changed: 141 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -869,29 +869,151 @@ Countly.Bulk = Bulk;
869869
* - pull, to remove value from array property
870870
* - addToSet, creates an array property, if property does not exist, and adds unique value to array, only if it does not yet exist in array
871871
************************* */
872-
var customData = {};
873-
var change_custom_property = function(key, value, mod) {
874-
key = cc.truncateSingleValue(key, Countly.maxKeyLength, "change_custom_property", Countly.debug);
875-
value = cc.truncateSingleValue(value, Countly.maxValueSize, "change_custom_property", Countly.debug);
876-
if (key === '__proto__' || key === 'constructor' || key === 'prototype') {
877-
cc.log(cc.logLevelEnums.ERROR, "change_custom_property, Provided key is not allowed.");
878-
return;
879-
}
872+
var userProperties = { custom: {} };
880873

881-
if (Countly.check_consent("users")) {
882-
if (!customData[key]) {
883-
customData[key] = {};
884-
}
885-
if (mod === "$push" || mod === "$pull" || mod === "$addToSet") {
886-
if (!customData[key][mod]) {
887-
customData[key][mod] = [];
874+
/**
875+
* Control user profile properties. Don't forget to call save after
876+
* @namespace Countly.userProfile
877+
* @name Countly.userProfile
878+
*/
879+
Countly.userProfile = {
880+
/**
881+
* Sets user's custom property value
882+
* @param {Object} user - Countly {@link UserDetails} object
883+
* @param {string=} user.name - user's full name
884+
* @param {string=} user.username - user's username or nickname
885+
* @param {string=} user.email - user's email address
886+
* @param {string=} user.organization - user's organization or company
887+
* @param {string=} user.phone - user's phone number
888+
* @param {string=} user.picture - url to user's picture
889+
* @param {string=} user.picturePath - local path to user's picture, if 'user.picture' is set this will be ignored
890+
* @param {string=} user.gender - M value for male and F value for female
891+
* @param {number=} user.byear - user's birth year used to calculate current age
892+
* @param {Object=} user.custom - object with custom key value properties you want to save with user
893+
*/
894+
set_properties(user) {
895+
cc.log(cc.logLevelEnums.INFO, "set_properties, Adding user details: ", user);
896+
if (Countly.check_consent("users")) {
897+
var props = ["name", "username", "email", "organization", "phone", "picture", "picturePath", "gender", "byear", "custom"];
898+
var extractedUserParams = cc.getProperties(user, props);
899+
for (var p in extractedUserParams) {
900+
if (p === "custom") {
901+
extractedUserParams.custom = cc.truncateObject(extractedUserParams.custom, Countly.maxKeyLength, Countly.maxValueSize, Countly.maxSegmentationValues, "set_properties");
902+
for (var c in extractedUserParams.custom) {
903+
userProperties.custom[c] = extractedUserParams.custom[c];
904+
}
905+
}
906+
else if (p !== "picture" && p === "picturePath") {
907+
userProperties[p] = extractedUserParams[p];
908+
}
909+
else {
910+
userProperties[p] = cc.truncateSingleValue(extractedUserParams[p], p !== "picture" ? Countly.maxValueSize : 4096, "set_properties", Countly.debug);
911+
}
888912
}
889-
customData[key][mod].push(value);
890913
}
891-
else {
892-
customData[key][mod] = value;
914+
},
915+
/**
916+
* Sets user's custom property value
917+
* @param {string} key - name of the property to attach to user
918+
* @param {string|number} value - value to store under provided property
919+
* */
920+
set_property(key, value) {
921+
Countly.userProfile.set_properties({ custom: { [key]: value } });
922+
},
923+
/**
924+
* Sets user's custom property value only if it was not set before
925+
* @param {string} key - name of the property to attach to user
926+
* @param {string|number} value - value to store under provided property
927+
* */
928+
set_once(key, value) {
929+
change_custom_property(key, value, "$setOnce");
930+
},
931+
/**
932+
* Unset's/delete's user's custom property
933+
* @param {string} key - name of the property to delete
934+
* */
935+
unset(key) {
936+
delete userProperties.custom[key];
937+
},
938+
/**
939+
* Increment value under the key of this user's custom properties by one
940+
* @param {string} key - name of the property to attach to user
941+
* */
942+
increment(key) {
943+
change_custom_property(key, 1, "$inc");
944+
},
945+
/**
946+
* Increment value under the key of this user's custom properties by provided value
947+
* @param {string} key - name of the property to attach to user
948+
* @param {number} value - value by which to increment server value
949+
* */
950+
increment_by(key, value) {
951+
change_custom_property(key, value, "$inc");
952+
},
953+
/**
954+
* Multiply value under the key of this user's custom properties by provided value
955+
* @param {string} key - name of the property to attach to user
956+
* @param {number} value - value by which to multiply server value
957+
* */
958+
multiply(key, value) {
959+
change_custom_property(key, value, "$mul");
960+
},
961+
/**
962+
* Save maximal value under the key of this user's custom properties
963+
* @param {string} key - name of the property to attach to user
964+
* @param {number} value - value which to compare to server's value and store maximal value of both provided
965+
* */
966+
max(key, value) {
967+
change_custom_property(key, value, "$max");
968+
},
969+
/**
970+
* Save minimal value under the key of this user's custom properties
971+
* @param {string} key - name of the property to attach to user
972+
* @param {number} value - value which to compare to server's value and store minimal value of both provided
973+
* */
974+
min(key, value) {
975+
change_custom_property(key, value, "$min");
976+
},
977+
/**
978+
* Add value to array under the key of this user's custom properties. If property is not an array, it will be converted to array
979+
* @param {string} key - name of the property to attach to user
980+
* @param {string|number} value - value which to add to array
981+
* */
982+
push(key, value) {
983+
change_custom_property(key, value, "$push");
984+
},
985+
/**
986+
* Add value to array under the key of this user's custom properties, storing only unique values. If property is not an array, it will be converted to array
987+
* @param {string} key - name of the property to attach to user
988+
* @param {string|number} value - value which to add to array
989+
* */
990+
push_unique(key, value) {
991+
change_custom_property(key, value, "$addToSet");
992+
},
993+
/**
994+
* Remove value from array under the key of this user's custom properties
995+
* @param {string} key - name of the property
996+
* @param {string|number} value - value which to remove from array
997+
* */
998+
pull(key, value) {
999+
change_custom_property(key, value, "$pull");
1000+
},
1001+
/**
1002+
* Save changes made to user's custom properties object and send them to server
1003+
* */
1004+
save() {
1005+
if (Countly.check_consent("users")) {
1006+
if (Object.keys(userProperties).length < 2 && Object.keys(userProperties.custom).length === 0) {
1007+
cc.log(cc.logLevelEnums.INFO, "save, No user properties to save.");
1008+
return;
1009+
}
1010+
if (userProperties.custom && Object.keys(userProperties.custom).length === 0) {
1011+
delete userProperties.custom;
1012+
}
1013+
toRequestQueue({ user_details: JSON.stringify(userProperties) });
8931014
}
894-
}
1015+
userProperties = { custom: {} };
1016+
},
8951017
};
8961018

8971019
/**

0 commit comments

Comments
 (0)