@mparticle/web-sdk
Advanced tools
Comparing version 2.9.13-rc.1 to 2.9.14-rc.1
@@ -5,2 +5,10 @@ ## Releases | ||
#### 2.9.14 - 2019-10-31 | ||
- Remove mparticle.js from root | ||
- Feat - Stub logBaseEvent in snippet, add snippet comments | ||
- Feat - Implement User Identity Change & User Attribute Change Events | ||
- Allow MediaEvents and Media PageEvents to be supported | ||
- Happy Halloween! :jack_o_lantern: | ||
#### 2.9.13 - 2019-10-09 | ||
@@ -7,0 +15,0 @@ |
{ | ||
"name": "@mparticle/web-sdk", | ||
"version": "2.9.13-rc.1", | ||
"version": "2.9.14-rc.1", | ||
"description": "mParticle core SDK for web applications", | ||
@@ -49,4 +49,4 @@ "license": "Apache-2.0", | ||
"watch": "ENVIRONMENT=dev BUILD=iife rollup --config rollup.config.js -w", | ||
"watch:all": "ENVIRONMENT=dev WATCHALL=true rollup --config rollup.config.js -w", | ||
"watch:tests": "ENVIRONMENT=dev rollup --config rollup.test.config.js -w", | ||
"watch:all": "ENVIRONMENT=prod BUILDALL=true rollup --config rollup.config.js -w", | ||
"watch:tests": "ENVIRONMENT=dev TESTTYPE=main rollup --config rollup.test.config.js -w", | ||
"lint": "eslint src/ test/src/", | ||
@@ -77,2 +77,3 @@ "gts:check": "gts check", | ||
"eslint-plugin-prettier": "3.1.1", | ||
"fetch-mock": "^7.5.1", | ||
"gts": "^1.1.0", | ||
@@ -79,0 +80,0 @@ "karma": "^4.0.1", |
var Constants = { | ||
sdkVersion: '2.9.13', | ||
sdkVersion: '2.9.14', | ||
sdkVendor: 'mparticle', | ||
@@ -4,0 +4,0 @@ platform: 'web', |
@@ -406,3 +406,3 @@ export interface ApplicationInformation { | ||
export type dentityType = | ||
export type identityType = | ||
| 'other' | ||
@@ -695,3 +695,3 @@ | 'customer_id' | ||
export interface UserIdentity { | ||
identity_type: dentityType; | ||
identity_type: identityType; | ||
identity: string; | ||
@@ -698,0 +698,0 @@ timestamp_unixtime_ms: number; |
@@ -15,3 +15,4 @@ import Helpers from './helpers'; | ||
HTTPCodes = Constants.HTTPCodes, | ||
sendIdentityRequest = ApiClient.sendIdentityRequest; | ||
sendIdentityRequest = ApiClient.sendIdentityRequest, | ||
sendEventToServer = ApiClient.sendEventToServer; | ||
@@ -801,4 +802,7 @@ function checkIdentitySwap(previousMPID, currentMPID, currentSessionMPIDs) { | ||
*/ | ||
setUserAttribute: function(key, value) { | ||
var cookies, userAttributes; | ||
setUserAttribute: function(key, newValue) { | ||
var cookies, | ||
userAttributes, | ||
previousUserAttributeValue, | ||
isNewAttribute; | ||
@@ -808,3 +812,3 @@ mParticle.sessionManager.resetSessionTimer(); | ||
if (Helpers.canLog()) { | ||
if (!Validators.isValidAttributeValue(value)) { | ||
if (!Validators.isValidAttributeValue(newValue)) { | ||
mParticle.Logger.error(Messages.ErrorMessages.BadAttribute); | ||
@@ -821,3 +825,3 @@ return; | ||
Constants.NativeSdkPaths.SetUserAttribute, | ||
JSON.stringify({ key: key, value: value }) | ||
JSON.stringify({ key: key, value: newValue }) | ||
); | ||
@@ -835,6 +839,19 @@ } else { | ||
if (existingProp) { | ||
isNewAttribute = false; | ||
previousUserAttributeValue = | ||
userAttributes[existingProp]; | ||
delete userAttributes[existingProp]; | ||
} else { | ||
isNewAttribute = true; | ||
} | ||
userAttributes[key] = value; | ||
sendUserAttributeChangeEvent( | ||
key, | ||
newValue, | ||
previousUserAttributeValue, | ||
isNewAttribute, | ||
false | ||
); | ||
userAttributes[key] = newValue; | ||
if (cookies && cookies[mpid]) { | ||
@@ -849,3 +866,3 @@ cookies[mpid].ua = userAttributes; | ||
); | ||
Forwarders.callSetUserAttributeOnForwarders(key, value); | ||
Forwarders.callSetUserAttributeOnForwarders(key, newValue); | ||
} | ||
@@ -900,2 +917,10 @@ } | ||
sendUserAttributeChangeEvent( | ||
key, | ||
null, | ||
userAttributes[key], | ||
false, | ||
true | ||
); | ||
var existingProp = Helpers.findKeyInObject(userAttributes, key); | ||
@@ -927,4 +952,8 @@ | ||
*/ | ||
setUserAttributeList: function(key, value) { | ||
var cookies, userAttributes; | ||
setUserAttributeList: function(key, newValue) { | ||
var cookies, | ||
userAttributes, | ||
previousUserAttributeValue, | ||
isNewAttribute, | ||
userAttributeChange; | ||
@@ -938,3 +967,3 @@ mParticle.sessionManager.resetSessionTimer(); | ||
if (!Array.isArray(value)) { | ||
if (!Array.isArray(newValue)) { | ||
mParticle.Logger.error( | ||
@@ -947,3 +976,3 @@ 'The value you passed in to setUserAttributeList must be an array. You passed in a ' + | ||
var arrayCopy = value.slice(); | ||
var arrayCopy = newValue.slice(); | ||
@@ -963,5 +992,42 @@ if (mParticle.Store.webviewBridgeEnabled) { | ||
if (existingProp) { | ||
isNewAttribute = false; | ||
previousUserAttributeValue = userAttributes[existingProp]; | ||
delete userAttributes[existingProp]; | ||
} else { | ||
isNewAttribute = true; | ||
} | ||
if (ApiClient.shouldEnableBatching()) { | ||
// If the new attributeList length is different previous, then there is a change event. | ||
// Loop through new attributes list, see if they are all in the same index as previous user attributes list | ||
// If there are any changes, break, and immediately send a userAttributeChangeEvent with full array as a value | ||
if ( | ||
!previousUserAttributeValue || | ||
!Array.isArray(previousUserAttributeValue) | ||
) { | ||
userAttributeChange = true; | ||
} else if ( | ||
newValue.length !== previousUserAttributeValue.length | ||
) { | ||
userAttributeChange = true; | ||
} else { | ||
for (var i = 0; i < newValue.length; i++) { | ||
if (previousUserAttributeValue[i] !== newValue[i]) { | ||
userAttributeChange = true; | ||
break; | ||
} | ||
} | ||
} | ||
if (userAttributeChange) { | ||
sendUserAttributeChangeEvent( | ||
key, | ||
newValue, | ||
previousUserAttributeValue, | ||
isNewAttribute, | ||
false | ||
); | ||
} | ||
} | ||
userAttributes[key] = arrayCopy; | ||
@@ -985,3 +1051,3 @@ if (cookies && cookies[mpid]) { | ||
removeAllUserAttributes: function() { | ||
var cookies, userAttributes; | ||
var userAttributes; | ||
@@ -995,4 +1061,2 @@ mParticle.sessionManager.resetSessionTimer(); | ||
} else { | ||
cookies = Persistence.getPersistence(); | ||
userAttributes = this.getAllUserAttributes(); | ||
@@ -1012,9 +1076,5 @@ | ||
} | ||
this.removeUserAttribute(prop); | ||
} | ||
} | ||
if (cookies && cookies[mpid]) { | ||
cookies[mpid].ua = {}; | ||
Persistence.saveCookies(cookies, mpid); | ||
} | ||
} | ||
@@ -1273,3 +1333,2 @@ }, | ||
indexOfMPID; | ||
var userIdentitiesForModify = {}, | ||
@@ -1295,2 +1354,8 @@ userIdentities = prevUser | ||
identityApiResult = JSON.parse(xhr.responseText); | ||
sendUserIdentityChange( | ||
identityApiData, | ||
method, | ||
identityApiResult.mpid | ||
); | ||
if (identityApiResult.hasOwnProperty('is_logged_in')) { | ||
@@ -1508,2 +1573,136 @@ mParticle.Store.isLoggedIn = identityApiResult.is_logged_in; | ||
// send a user identity change request on identify, login, logout, modify when any values change. | ||
// compare what identities exist vs what it previously was for the specific user if they were in memory before. | ||
// if it's the first time the user is logging in, send a user identity change request with created_this_batch = true | ||
// created_this_batch is always false for old user | ||
function sendUserIdentityChange(newIdentityApiData, method, mpid) { | ||
var userInMemory, userIdentitiesInMemory, userIdentityChangeEvent; | ||
if (!ApiClient.shouldEnableBatching()) { | ||
return; | ||
} | ||
if (!mpid) { | ||
if (method !== 'modify') { | ||
return; | ||
} | ||
} | ||
userInMemory = | ||
method === 'modify' | ||
? IdentityAPI.getCurrentUser() | ||
: IdentityAPI.getUser(mpid); | ||
var newUserIdentities = newIdentityApiData.userIdentities; | ||
// if there is not a user in memory with this mpid, then it is a new user, and we send a user identity | ||
// change for each identity on the identity api request | ||
if (userInMemory) { | ||
userIdentitiesInMemory = userInMemory.getUserIdentities() | ||
? userInMemory.getUserIdentities().userIdentities | ||
: {}; | ||
} else { | ||
for (var identityType in newUserIdentities) { | ||
userIdentityChangeEvent = createUserIdentityChange( | ||
identityType, | ||
newUserIdentities[identityType], | ||
null, | ||
true | ||
); | ||
sendEventToServer(userIdentityChangeEvent); | ||
} | ||
return; | ||
} | ||
for (identityType in newUserIdentities) { | ||
if ( | ||
userIdentitiesInMemory[identityType] !== | ||
newUserIdentities[identityType] | ||
) { | ||
var isNewUserIdentityType = !userIdentitiesInMemory[identityType]; | ||
userIdentityChangeEvent = createUserIdentityChange( | ||
identityType, | ||
newUserIdentities[identityType], | ||
userIdentitiesInMemory[identityType], | ||
isNewUserIdentityType | ||
); | ||
sendEventToServer(userIdentityChangeEvent); | ||
} | ||
} | ||
} | ||
function createUserIdentityChange( | ||
identityType, | ||
newIdentity, | ||
oldIdentity, | ||
newCreatedThisBatch | ||
) { | ||
var userIdentityChangeEvent; | ||
userIdentityChangeEvent = ServerModel.createEventObject({ | ||
messageType: Types.MessageType.UserIdentityChange, | ||
userIdentityChanges: { | ||
New: { | ||
IdentityType: identityType, | ||
Identity: newIdentity, | ||
CreatedThisBatch: newCreatedThisBatch, | ||
}, | ||
Old: { | ||
IdentityType: identityType, | ||
Identity: oldIdentity, | ||
CreatedThisBatch: false, | ||
}, | ||
}, | ||
}); | ||
return userIdentityChangeEvent; | ||
} | ||
function sendUserAttributeChangeEvent( | ||
attributeKey, | ||
newUserAttributeValue, | ||
previousUserAttributeValue, | ||
isNewAttribute, | ||
deleted | ||
) { | ||
if (!ApiClient.shouldEnableBatching()) { | ||
return; | ||
} | ||
var userAttributeChangeEvent = createUserAttributeChange( | ||
attributeKey, | ||
newUserAttributeValue, | ||
previousUserAttributeValue, | ||
isNewAttribute, | ||
deleted | ||
); | ||
if (userAttributeChangeEvent) { | ||
sendEventToServer(userAttributeChangeEvent); | ||
} | ||
} | ||
function createUserAttributeChange( | ||
key, | ||
newValue, | ||
previousUserAttributeValue, | ||
isNewAttribute, | ||
deleted | ||
) { | ||
if (!previousUserAttributeValue) { | ||
previousUserAttributeValue = null; | ||
} | ||
var userAttributeChangeEvent; | ||
if (newValue !== previousUserAttributeValue) { | ||
userAttributeChangeEvent = ServerModel.createEventObject({ | ||
messageType: Types.MessageType.UserAttributeChange, | ||
userAttributeChanges: { | ||
UserAttributeName: key, | ||
New: newValue, | ||
Old: previousUserAttributeValue || null, | ||
Deleted: deleted, | ||
IsNewAttribute: isNewAttribute, | ||
}, | ||
}); | ||
} | ||
return userAttributeChangeEvent; | ||
} | ||
export default { | ||
@@ -1510,0 +1709,0 @@ checkIdentitySwap: checkIdentitySwap, |
@@ -277,4 +277,4 @@ // | ||
if (!event.type) { | ||
event.type = Types.EventType.Unknown; | ||
if (!event.eventType) { | ||
event.eventType = Types.EventType.Unknown; | ||
} | ||
@@ -1065,7 +1065,8 @@ | ||
function processPreloadedItem(readyQueueItem) { | ||
var currentUser, | ||
args = readyQueueItem, | ||
var args = readyQueueItem, | ||
method = args.splice(0, 1)[0]; | ||
// if the first argument is a method on the base mParticle object, run it | ||
if (mParticle[args[0]]) { | ||
mParticle[method].apply(this, args); | ||
// otherwise, the method is on either eCommerce or Identity objects, ie. "eCommerce.setCurrencyCode", "Identity.login" | ||
} else { | ||
@@ -1079,3 +1080,3 @@ var methodArray = method.split('.'); | ||
} | ||
computedMPFunction.apply(currentUser, args); | ||
computedMPFunction.apply(this, args); | ||
} catch (e) { | ||
@@ -1082,0 +1083,0 @@ mParticle.Logger.verbose( |
@@ -0,1 +1,3 @@ | ||
import * as EventsApi from './eventsApiModels'; | ||
export interface SDKEvent { | ||
@@ -28,2 +30,4 @@ DeviceId: string; | ||
ShoppingCart?: SDKShoppingCart; | ||
UserIdentityChanges?: SDKUserIdentityChangeData; | ||
UserAttributeChanges?: SDKUserAttributeChangeData; | ||
CurrencyCode: string; | ||
@@ -36,5 +40,7 @@ } | ||
} | ||
export interface SDKShoppingCart { | ||
ProductList?: SDKProduct[]; | ||
} | ||
export interface SDKPromotionAction { | ||
@@ -141,1 +147,21 @@ PromotionActionType: string; | ||
} | ||
export interface SDKUserIdentityChangeData { | ||
New: Identity; | ||
Old: Identity; | ||
} | ||
export interface Identity { | ||
IdentityType: EventsApi.identityType; | ||
Identity: string; | ||
Timestamp: number; | ||
CreatedThisBatch: boolean; | ||
} | ||
export interface SDKUserAttributeChangeData { | ||
UserAttributeName: string; | ||
New: string; | ||
Old: string; | ||
Deleted: boolean; | ||
IsNewAttribute: boolean; | ||
} |
@@ -167,2 +167,6 @@ import { | ||
return convertSessionStartEvent(sdkEvent); | ||
case Types.MessageType.UserAttributeChange: | ||
return convertUserAttributeChangeEvent(sdkEvent); | ||
case Types.MessageType.UserIdentityChange: | ||
return convertUserIdentityChangeEvent(sdkEvent); | ||
default: | ||
@@ -526,1 +530,61 @@ break; | ||
} | ||
export function convertUserAttributeChangeEvent( | ||
sdkEvent: SDKEvent | ||
): EventsApi.UserAttributeChangeEvent | null { | ||
const commonEventData: EventsApi.CommonEventData = convertBaseEventData( | ||
sdkEvent | ||
); | ||
let userAttributeChangeEvent: EventsApi.UserAttributeChangeEventData = { | ||
user_attribute_name: sdkEvent.UserAttributeChanges.UserAttributeName, | ||
new: sdkEvent.UserAttributeChanges.New, | ||
old: sdkEvent.UserAttributeChanges.Old, | ||
deleted: sdkEvent.UserAttributeChanges.Deleted, | ||
is_new_attribute: sdkEvent.UserAttributeChanges.IsNewAttribute, | ||
}; | ||
userAttributeChangeEvent = { | ||
...userAttributeChangeEvent, | ||
...commonEventData, | ||
}; | ||
return { | ||
event_type: 'user_attribute_change', | ||
data: userAttributeChangeEvent, | ||
}; | ||
} | ||
export function convertUserIdentityChangeEvent( | ||
sdkEvent: SDKEvent | ||
): EventsApi.UserIdentityChangeEvent | null { | ||
const commonEventData: EventsApi.CommonEventData = convertBaseEventData( | ||
sdkEvent | ||
); | ||
let userIdentityChangeEvent: EventsApi.UserIdentityChangeEventData = { | ||
new: { | ||
identity_type: sdkEvent.UserIdentityChanges.New.IdentityType, | ||
identity: sdkEvent.UserIdentityChanges.New.Identity || null, | ||
timestamp_unixtime_ms: sdkEvent.Timestamp, | ||
created_this_batch: | ||
sdkEvent.UserIdentityChanges.New.CreatedThisBatch, | ||
}, | ||
old: { | ||
identity_type: sdkEvent.UserIdentityChanges.Old.IdentityType, | ||
identity: sdkEvent.UserIdentityChanges.Old.Identity || null, | ||
timestamp_unixtime_ms: sdkEvent.Timestamp, | ||
created_this_batch: | ||
sdkEvent.UserIdentityChanges.Old.CreatedThisBatch, | ||
}, | ||
}; | ||
userIdentityChangeEvent = Object.assign( | ||
userIdentityChangeEvent, | ||
commonEventData | ||
); | ||
return { | ||
event_type: 'user_identity_change', | ||
data: userIdentityChangeEvent, | ||
}; | ||
} |
@@ -166,2 +166,4 @@ import Types from './types'; | ||
CustomFlags: event.customFlags || {}, | ||
UserAttributeChanges: event.userAttributeChanges, | ||
UserIdentityChanges: event.userIdentityChanges, | ||
}; | ||
@@ -168,0 +170,0 @@ } |
@@ -12,2 +12,4 @@ var MessageType = { | ||
Media: 20, | ||
UserAttributeChange: 17, | ||
UserIdentityChange: 18, | ||
}; | ||
@@ -25,2 +27,3 @@ | ||
Other: 8, | ||
Media: 9, | ||
getName: function(id) { | ||
@@ -27,0 +30,0 @@ switch (id) { |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
810087
14029
45