Skip to content

Commit f98d4aa

Browse files
djihblazzy
authored andcommitted
support groupIdentify calls (#151)
1 parent 94ab412 commit f98d4aa

8 files changed

+297
-28
lines changed

amplitude-segment-snippet.min.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ var i=function(){this._q=[];return this}
77
;return this}
88
;var u=["setProductId","setQuantity","setPrice","setRevenueType","setEventProperties"]
99
;for(var c=0;c<u.length;c++){r(a,u[c])}n.Revenue=a
10-
;var l=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties","setGroup","logRevenueV2","regenerateDeviceId","logEventWithTimestamp","logEventWithGroups","setSessionId","resetSessionId"]
11-
;function p(e){function t(t){e[t]=function(){
10+
;var p=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties","setGroup","logRevenueV2","regenerateDeviceId","groupIdentify","logEventWithTimestamp","logEventWithGroups","setSessionId","resetSessionId"]
11+
;function l(e){function t(t){e[t]=function(){
1212
e._q.push([t].concat(Array.prototype.slice.call(arguments,0)))}}
13-
for(var n=0;n<l.length;n++){t(l[n])}}p(n);n.getInstance=function(e){
13+
for(var n=0;n<p.length;n++){t(p[n])}}l(n);n.getInstance=function(e){
1414
e=(!e||e.length===0?"$default_instance":e).toLowerCase()
15-
;if(!n._iq.hasOwnProperty(e)){n._iq[e]={_q:[]};p(n._iq[e])}return n._iq[e]}
15+
;if(!n._iq.hasOwnProperty(e)){n._iq[e]={_q:[]};l(n._iq[e])}return n._iq[e]}
1616
;e.amplitude=n})(window,document);

amplitude-snippet.min.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var o=function(){this._q=[];return this}
1313
;return this}
1414
;var l=["setProductId","setQuantity","setPrice","setRevenueType","setEventProperties"]
1515
;for(var p=0;p<l.length;p++){s(c,l[p])}n.Revenue=c
16-
;var d=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties","setGroup","logRevenueV2","regenerateDeviceId","logEventWithTimestamp","logEventWithGroups","setSessionId","resetSessionId"]
16+
;var d=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties","setGroup","logRevenueV2","regenerateDeviceId","groupIdentify","logEventWithTimestamp","logEventWithGroups","setSessionId","resetSessionId"]
1717
;function v(e){function t(t){e[t]=function(){
1818
e._q.push([t].concat(Array.prototype.slice.call(arguments,0)))}}
1919
for(var n=0;n<d.length;n++){t(d[n])}}v(n);n.getInstance=function(e){

amplitude.js

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,7 @@ var constants = {
925925
MAX_STRING_LENGTH: 4096,
926926
MAX_PROPERTY_KEYS: 1000,
927927
IDENTIFY_EVENT: '$identify',
928+
GROUP_IDENTIFY_EVENT: '$groupidentify',
928929

929930
// localStorageKeys
930931
LAST_EVENT_ID: 'amplitude_lastEventId',
@@ -3977,7 +3978,7 @@ var defineProperty = (function() {
39773978
} catch (e) {}
39783979
}());
39793980

3980-
var _defineProperty = defineProperty;
3981+
var _defineProperty$1 = defineProperty;
39813982

39823983
/**
39833984
* The base implementation of `assignValue` and `assignMergeValue` without
@@ -3989,8 +3990,8 @@ var _defineProperty = defineProperty;
39893990
* @param {*} value The value to assign.
39903991
*/
39913992
function baseAssignValue(object, key, value) {
3992-
if (key == '__proto__' && _defineProperty) {
3993-
_defineProperty(object, key, {
3993+
if (key == '__proto__' && _defineProperty$1) {
3994+
_defineProperty$1(object, key, {
39943995
'configurable': true,
39953996
'enumerable': true,
39963997
'value': value,
@@ -5167,8 +5168,8 @@ var constant_1 = constant;
51675168
* @param {Function} string The `toString` result.
51685169
* @returns {Function} Returns `func`.
51695170
*/
5170-
var baseSetToString = !_defineProperty ? identity_1 : function(func, string) {
5171-
return _defineProperty(func, 'toString', {
5171+
var baseSetToString = !_defineProperty$1 ? identity_1 : function(func, string) {
5172+
return _defineProperty$1(func, 'toString', {
51725173
'configurable': true,
51735174
'enumerable': false,
51745175
'value': constant_1(string),
@@ -6665,6 +6666,8 @@ var DEFAULT_OPTIONS = {
66656666
uploadBatchSize: 100
66666667
};
66676668

6669+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6670+
66686671
/**
66696672
* AmplitudeClient SDK API - instance constructor.
66706673
* The Amplitude class handles creation of client instances, all you need to do is call amplitude.getInstance()
@@ -7321,7 +7324,7 @@ AmplitudeClient.prototype.setGroup = function (groupType, groupName) {
73217324
var groups = {};
73227325
groups[groupType] = groupName;
73237326
var identify = new Identify().set(groupType, groupName);
7324-
this._logEvent(constants.IDENTIFY_EVENT, null, null, identify.userPropertiesOperations, groups, null, null);
7327+
this._logEvent(constants.IDENTIFY_EVENT, null, null, identify.userPropertiesOperations, groups, null, null, null);
73257328
};
73267329

73277330
/**
@@ -7479,7 +7482,7 @@ AmplitudeClient.prototype.identify = function (identify_obj, opt_callback) {
74797482
if (identify_obj instanceof Identify) {
74807483
// only send if there are operations
74817484
if (Object.keys(identify_obj.userPropertiesOperations).length > 0) {
7482-
return this._logEvent(constants.IDENTIFY_EVENT, null, null, identify_obj.userPropertiesOperations, null, null, opt_callback);
7485+
return this._logEvent(constants.IDENTIFY_EVENT, null, null, identify_obj.userPropertiesOperations, null, null, null, opt_callback);
74837486
} else {
74847487
if (type(opt_callback) === 'function') {
74857488
opt_callback(0, 'No request sent', { reason: 'No user property operations' });
@@ -7493,6 +7496,50 @@ AmplitudeClient.prototype.identify = function (identify_obj, opt_callback) {
74937496
}
74947497
};
74957498

7499+
AmplitudeClient.prototype.groupIdentify = function (group_type, group_name, identify_obj, opt_callback) {
7500+
if (!this._apiKeySet('groupIdentify()')) {
7501+
if (type(opt_callback) === 'function') {
7502+
opt_callback(0, 'No request sent', { reason: 'API key is not set' });
7503+
}
7504+
return;
7505+
}
7506+
7507+
if (!utils.validateInput(group_type, 'group_type', 'string') || utils.isEmptyString(group_type)) {
7508+
if (type(opt_callback) === 'function') {
7509+
opt_callback(0, 'No request sent', { reason: 'Invalid group type' });
7510+
}
7511+
return;
7512+
}
7513+
7514+
if (group_name === null || group_name === undefined) {
7515+
if (type(opt_callback) === 'function') {
7516+
opt_callback(0, 'No request sent', { reason: 'Invalid group name' });
7517+
}
7518+
return;
7519+
}
7520+
7521+
// if identify input is a proxied object created by the async loading snippet, convert it into an identify object
7522+
if (type(identify_obj) === 'object' && identify_obj.hasOwnProperty('_q')) {
7523+
identify_obj = _convertProxyObjectToRealObject(new Identify(), identify_obj);
7524+
}
7525+
7526+
if (identify_obj instanceof Identify) {
7527+
// only send if there are operations
7528+
if (Object.keys(identify_obj.userPropertiesOperations).length > 0) {
7529+
return this._logEvent(constants.GROUP_IDENTIFY_EVENT, null, null, null, _defineProperty({}, group_type, group_name), identify_obj.userPropertiesOperations, null, opt_callback);
7530+
} else {
7531+
if (type(opt_callback) === 'function') {
7532+
opt_callback(0, 'No request sent', { reason: 'No group property operations' });
7533+
}
7534+
}
7535+
} else {
7536+
utils.log.error('Invalid identify input type. Expected Identify object but saw ' + type(identify_obj));
7537+
if (type(opt_callback) === 'function') {
7538+
opt_callback(0, 'No request sent', { reason: 'Invalid identify input type' });
7539+
}
7540+
}
7541+
};
7542+
74967543
/**
74977544
* Set a versionName for your application.
74987545
* @public
@@ -7510,7 +7557,7 @@ AmplitudeClient.prototype.setVersionName = function setVersionName(versionName)
75107557
* Private logEvent method. Keeps apiProperties from being publicly exposed.
75117558
* @private
75127559
*/
7513-
AmplitudeClient.prototype._logEvent = function _logEvent(eventType, eventProperties, apiProperties, userProperties, groups, timestamp, callback) {
7560+
AmplitudeClient.prototype._logEvent = function _logEvent(eventType, eventProperties, apiProperties, userProperties, groups, groupProperties, timestamp, callback) {
75147561
_loadCookieData(this); // reload cookie before each log event to sync event meta-data between windows and tabs
75157562
if (!eventType) {
75167563
if (type(callback) === 'function') {
@@ -7527,7 +7574,7 @@ AmplitudeClient.prototype._logEvent = function _logEvent(eventType, eventPropert
75277574

75287575
try {
75297576
var eventId;
7530-
if (eventType === constants.IDENTIFY_EVENT) {
7577+
if (eventType === constants.IDENTIFY_EVENT || eventType === constants.GROUP_IDENTIFY_EVENT) {
75317578
eventId = this.nextIdentifyId();
75327579
} else {
75337580
eventId = this.nextEventId();
@@ -7568,10 +7615,11 @@ AmplitudeClient.prototype._logEvent = function _logEvent(eventType, eventPropert
75687615
},
75697616
sequence_number: sequenceNumber, // for ordering events and identifys
75707617
groups: utils.truncate(utils.validateGroups(groups)),
7618+
group_properties: utils.truncate(utils.validateProperties(groupProperties)),
75717619
user_agent: this._userAgent
75727620
};
75737621

7574-
if (eventType === constants.IDENTIFY_EVENT) {
7622+
if (eventType === constants.IDENTIFY_EVENT || eventType === constants.GROUP_IDENTIFY_EVENT) {
75757623
this._unsentIdentifys.push(event);
75767624
this._limitEventsQueued(this._unsentIdentifys);
75777625
} else {
@@ -7670,7 +7718,7 @@ AmplitudeClient.prototype.logEventWithTimestamp = function logEvent(eventType, e
76707718
}
76717719
return -1;
76727720
}
7673-
return this._logEvent(eventType, eventProperties, null, null, null, timestamp, opt_callback);
7721+
return this._logEvent(eventType, eventProperties, null, null, null, null, timestamp, opt_callback);
76747722
};
76757723

76767724
/**
@@ -7701,7 +7749,7 @@ AmplitudeClient.prototype.logEventWithGroups = function (eventType, eventPropert
77017749
}
77027750
return -1;
77037751
}
7704-
return this._logEvent(eventType, eventProperties, null, null, groups, null, opt_callback);
7752+
return this._logEvent(eventType, eventProperties, null, null, groups, null, null, opt_callback);
77057753
};
77067754

77077755
/**
@@ -7763,7 +7811,7 @@ AmplitudeClient.prototype.logRevenue = function logRevenue(price, quantity, prod
77637811
special: 'revenue_amount',
77647812
quantity: quantity || 1,
77657813
price: price
7766-
}, null, null, null, null);
7814+
}, null, null, null, null, null);
77677815
};
77687816

77697817
/**

amplitude.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/amplitude-client.js

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ AmplitudeClient.prototype.setGroup = function(groupType, groupName) {
678678
var groups = {};
679679
groups[groupType] = groupName;
680680
var identify = new Identify().set(groupType, groupName);
681-
this._logEvent(Constants.IDENTIFY_EVENT, null, null, identify.userPropertiesOperations, groups, null, null);
681+
this._logEvent(Constants.IDENTIFY_EVENT, null, null, identify.userPropertiesOperations, groups, null, null, null);
682682
};
683683

684684
/**
@@ -837,7 +837,7 @@ AmplitudeClient.prototype.identify = function(identify_obj, opt_callback) {
837837
// only send if there are operations
838838
if (Object.keys(identify_obj.userPropertiesOperations).length > 0) {
839839
return this._logEvent(
840-
Constants.IDENTIFY_EVENT, null, null, identify_obj.userPropertiesOperations, null, null, opt_callback
840+
Constants.IDENTIFY_EVENT, null, null, identify_obj.userPropertiesOperations, null, null, null, opt_callback
841841
);
842842
} else {
843843
if (type(opt_callback) === 'function') {
@@ -852,6 +852,53 @@ AmplitudeClient.prototype.identify = function(identify_obj, opt_callback) {
852852
}
853853
};
854854

855+
AmplitudeClient.prototype.groupIdentify = function(group_type, group_name, identify_obj, opt_callback) {
856+
if (!this._apiKeySet('groupIdentify()')) {
857+
if (type(opt_callback) === 'function') {
858+
opt_callback(0, 'No request sent', {reason: 'API key is not set'});
859+
}
860+
return;
861+
}
862+
863+
if (!utils.validateInput(group_type, 'group_type', 'string') ||
864+
utils.isEmptyString(group_type)) {
865+
if (type(opt_callback) === 'function') {
866+
opt_callback(0, 'No request sent', {reason: 'Invalid group type'});
867+
}
868+
return;
869+
}
870+
871+
if (group_name === null || group_name === undefined) {
872+
if (type(opt_callback) === 'function') {
873+
opt_callback(0, 'No request sent', {reason: 'Invalid group name'});
874+
}
875+
return;
876+
}
877+
878+
// if identify input is a proxied object created by the async loading snippet, convert it into an identify object
879+
if (type(identify_obj) === 'object' && identify_obj.hasOwnProperty('_q')) {
880+
identify_obj = _convertProxyObjectToRealObject(new Identify(), identify_obj);
881+
}
882+
883+
if (identify_obj instanceof Identify) {
884+
// only send if there are operations
885+
if (Object.keys(identify_obj.userPropertiesOperations).length > 0) {
886+
return this._logEvent(
887+
Constants.GROUP_IDENTIFY_EVENT, null, null, null, {[group_type]: group_name}, identify_obj.userPropertiesOperations, null, opt_callback
888+
);
889+
} else {
890+
if (type(opt_callback) === 'function') {
891+
opt_callback(0, 'No request sent', {reason: 'No group property operations'});
892+
}
893+
}
894+
} else {
895+
utils.log.error('Invalid identify input type. Expected Identify object but saw ' + type(identify_obj));
896+
if (type(opt_callback) === 'function') {
897+
opt_callback(0, 'No request sent', {reason: 'Invalid identify input type'});
898+
}
899+
}
900+
};
901+
855902
/**
856903
* Set a versionName for your application.
857904
* @public
@@ -869,7 +916,7 @@ AmplitudeClient.prototype.setVersionName = function setVersionName(versionName)
869916
* Private logEvent method. Keeps apiProperties from being publicly exposed.
870917
* @private
871918
*/
872-
AmplitudeClient.prototype._logEvent = function _logEvent(eventType, eventProperties, apiProperties, userProperties, groups, timestamp, callback) {
919+
AmplitudeClient.prototype._logEvent = function _logEvent(eventType, eventProperties, apiProperties, userProperties, groups, groupProperties, timestamp, callback) {
873920
_loadCookieData(this); // reload cookie before each log event to sync event meta-data between windows and tabs
874921
if (!eventType) {
875922
if (type(callback) === 'function') {
@@ -886,7 +933,7 @@ AmplitudeClient.prototype._logEvent = function _logEvent(eventType, eventPropert
886933

887934
try {
888935
var eventId;
889-
if (eventType === Constants.IDENTIFY_EVENT) {
936+
if (eventType === Constants.IDENTIFY_EVENT || eventType === Constants.GROUP_IDENTIFY_EVENT) {
890937
eventId = this.nextIdentifyId();
891938
} else {
892939
eventId = this.nextEventId();
@@ -927,10 +974,11 @@ AmplitudeClient.prototype._logEvent = function _logEvent(eventType, eventPropert
927974
},
928975
sequence_number: sequenceNumber, // for ordering events and identifys
929976
groups: utils.truncate(utils.validateGroups(groups)),
977+
group_properties: utils.truncate(utils.validateProperties(groupProperties)),
930978
user_agent: this._userAgent
931979
};
932980

933-
if (eventType === Constants.IDENTIFY_EVENT) {
981+
if (eventType === Constants.IDENTIFY_EVENT || eventType === Constants.GROUP_IDENTIFY_EVENT) {
934982
this._unsentIdentifys.push(event);
935983
this._limitEventsQueued(this._unsentIdentifys);
936984
} else {
@@ -1029,7 +1077,7 @@ AmplitudeClient.prototype.logEventWithTimestamp = function logEvent(eventType, e
10291077
}
10301078
return -1;
10311079
}
1032-
return this._logEvent(eventType, eventProperties, null, null, null, timestamp, opt_callback);
1080+
return this._logEvent(eventType, eventProperties, null, null, null, null, timestamp, opt_callback);
10331081
};
10341082

10351083
/**
@@ -1060,7 +1108,7 @@ AmplitudeClient.prototype.logEventWithGroups = function(eventType, eventProperti
10601108
}
10611109
return -1;
10621110
}
1063-
return this._logEvent(eventType, eventProperties, null, null, groups, null, opt_callback);
1111+
return this._logEvent(eventType, eventProperties, null, null, groups, null, null, opt_callback);
10641112
};
10651113

10661114
/**
@@ -1122,7 +1170,7 @@ AmplitudeClient.prototype.logRevenue = function logRevenue(price, quantity, prod
11221170
special: 'revenue_amount',
11231171
quantity: quantity || 1,
11241172
price: price
1125-
}, null, null, null, null);
1173+
}, null, null, null, null, null);
11261174
};
11271175

11281176
/**

src/amplitude-snippet.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
var funcs = ['init', 'logEvent', 'logRevenue', 'setUserId', 'setUserProperties',
2424
'setOptOut', 'setVersionName', 'setDomain', 'setDeviceId',
2525
'setGlobalUserProperties', 'identify', 'clearUserProperties',
26-
'setGroup', 'logRevenueV2', 'regenerateDeviceId',
26+
'setGroup', 'logRevenueV2', 'regenerateDeviceId', 'groupIdentify',
2727
'logEventWithTimestamp', 'logEventWithGroups', 'setSessionId', 'resetSessionId'];
2828
function setUpProxy(instance) {
2929
function proxyMain(fn) {

src/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export default {
44
MAX_STRING_LENGTH: 4096,
55
MAX_PROPERTY_KEYS: 1000,
66
IDENTIFY_EVENT: '$identify',
7+
GROUP_IDENTIFY_EVENT: '$groupidentify',
78

89
// localStorageKeys
910
LAST_EVENT_ID: 'amplitude_lastEventId',

0 commit comments

Comments
 (0)