Skip to content

Commit

Permalink
Draft update
Browse files Browse the repository at this point in the history
  • Loading branch information
SbDove committed Sep 25, 2024
1 parent 12dbc44 commit 36c72fe
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 134 deletions.
79 changes: 78 additions & 1 deletion src/common.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,84 @@
var SHA256 = require('crypto-js/sha256');
function Common() {}

Common.prototype.exampleMethod = function () {
return 'I am an example';
}

module.exports = Common;
Common.prototype.logId5 = function () {
var id5Id = this.id5Instance.getUserId();
debugger;

//Checks and saves ID5 ID if it is new
if (id5Id != this.id5Id) {
this.id5Id = id5Id;
this.id5IdSent = false
}

//Only sends the ID5 ID Custom Event if unsent
if (this.id5IdSent == false){
var currentUser = mParticle.Identity.getCurrentUser();
currentUser.setUserAttribute('ID5ID', id5Id);
this.id5IdSent = true;
}
};

Common.prototype.buildPartnerData = function (mParticleUser) {
var pdKeys = {};
var userIdentities = mParticleUser.getUserIdentities();

var email = userIdentities.userIdentities['email'];
if (email) {
var processedEmail = normalizeAndHashEmail(email);
pdKeys[1] = processedEmail;
}

var phone = userIdentities.userIdentities['mobile_number'];
if (phone) {
var processedPhone = normalizeAndHashPhone(phone);
pdKeys[2] = processedPhone;
}

//Candidates to be removed
var fullUrl = window.location.href;
if (fullUrl) pdKeys[8] = encodeURIComponent(fullUrl);

var domain = window.location.host;
if (domain) pdKeys[9] = domain;

var userAgentString = navigator.userAgent;
if (userAgentString) pdKeys[12] = encodeURIComponent(userAgentString);
// above may be pulled from PD as they are un needed for the ID5 ID generation as a basic level


var pdRaw = Object.keys(pdKeys).map(function(key){
return key + '=' + pdKeys[key]
}).join('&');

return btoa(pdRaw);
}

function normalizeAndHashEmail(email) {
var parts = email.split("@")
var charactersToRemove = ['+', '.']

if (parts[1] != 'gmail.com') {
return email;
}

charactersToRemove.forEach(function(character) {
parts[0] = parts[0].replaceAll(character, '').toLowerCase();
})

return SHA256(parts.join('@'));
}

function normalizeAndHashPhone(phone) {
var charactersToRemove = [' ', '-', '(', ')']
charactersToRemove.forEach(function(character) {
phone = phone.replaceAll(character, '');
})
return SHA256(phone);
}

module.exports = Common;
5 changes: 4 additions & 1 deletion src/event-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ A non-ecommerce event has the following schema:
function EventHandler(common) {
this.common = common || {};
}
EventHandler.prototype.logEvent = function(event) {};
EventHandler.prototype.logEvent = function() {
this.common.logId5();
};

EventHandler.prototype.logError = function(event) {
// The schema for a logError event is the same, but noteworthy differences are as follows:
// {
Expand Down
15 changes: 12 additions & 3 deletions src/identity-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,28 @@ IdentityHandler.prototype.onUserIdentified = function(mParticleUser) {};
IdentityHandler.prototype.onIdentifyComplete = function(
mParticleUser,
identityApiRequest
) {};
) {
var partnerData = this.common.buildPartnerData(mParticleUser);
this.common.id5Instance = window.ID5.init({partnerId: this.common.partnerId, pd: partnerData})
};
IdentityHandler.prototype.onLoginComplete = function(
mParticleUser,
identityApiRequest
) {};
IdentityHandler.prototype.onLogoutComplete = function(
mParticleUser,
identityApiRequest
) {};
) {
var partnerData = this.common.buildPartnerData(mParticleUser);
this.common.id5Instance = window.ID5.init({partnerId: this.common.partnerId, pd: partnerData})
};
IdentityHandler.prototype.onModifyComplete = function(
mParticleUser,
identityApiRequest
) {};
) {
var partnerData = this.common.buildPartnerData(mParticleUser);
this.common.id5Instance = window.ID5.init({partnerId: this.common.partnerId, pd: partnerData})
};

/* In previous versions of the mParticle web SDK, setting user identities on
kits is only reachable via the onSetUserIdentity method below. We recommend
Expand Down
88 changes: 12 additions & 76 deletions src/initialization.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// import SHA256 from 'crypto-js/sha256';

var initialization = {
name: 'ID5',
moduleId: '248',
Expand All @@ -11,31 +9,25 @@ var initialization = {
userIdentities example: { 1: 'customerId', 2: 'facebookId', 7: '[email protected]' }
additional identityTypes can be found at https://github.com/mParticle/mparticle-sdk-javascript/blob/master-v2/src/types.js#L88-L101
*/
initForwarder: function(forwarderSettings, testMode, userAttributes, userIdentities, processEvent, eventQueue, isInitialized, common, appVersion, appName, customFlags, clientId) {
initForwarder: function(forwarderSettings, testMode, userAttributes, userIdentities, processEvent, eventQueue, isInitialized, common) {
/* `forwarderSettings` contains your SDK specific settings such as apiKey that your customer needs in order to initialize your SDK properly */

if (!testMode) {
/* Load your Web SDK here using a variant of your snippet from your readme that your customers would generally put into their <head> tags
Generally, our integrations create script tags and append them to the <head>. Please follow the following format as a guide:
*/

var clientScript = document.createElement('script');
clientScript.type = 'text/javascript';
clientScript.async = true;
clientScript.src = 'https://cdn.id5-sync.com/api/1.0/id5-api.js'; // <---- Update this to be your script
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(clientScript);
clientScript.onload = function() {
var partnerData = buildPartnerData(userIdentities);

//To-Do: PartnerUserId to be added to the init options once received from id5
window.ID5.init({partnerId: forwarderSettings.partnerId, pd: partnerData}).onAvailable(function(status) {
var id5Id = status.getID5Id();
var idType = forwarderSettings.id5IdType;
var identities = {};
identities[idType] = id5Id;

window.mParticle.Identity.modify({userIdentities: identities}, identityCallback)
});
var id5Script = document.createElement('script');
id5Script.src = 'https://cdn.id5-sync.com/api/1.0/id5-api.js'; // <---- Update this to be your script
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(id5Script);
debugger;
common.id5Id = null;
common.id5IdSent = false;
common.parterId = forwarderSettings.partnerId;

id5Script.onload = function() {
isInitialized = true;
common.id5Instance = window.ID5.init({partnerId: forwarderSettings.partnerId})
};
} else {
// For testing, you should fill out this section in order to ensure any required initialization calls are made,
Expand All @@ -44,60 +36,4 @@ var initialization = {
}
};


function buildPartnerData(userIdentities) {
var pdKeys = {};

var email = userIdentities.userIdentities['email'];
var processedEmail = normalizeAndHashEmail(email);
if (processedEmail) pdKeys[1] = processedEmail;

var phone = userIdentities.userIdentities['mobile_number'];
var processedPhone = normalizeAndHashPhone(phone);
if (processedPhone) pdKeys[2] = processedPhone;

var fullUrl = window.location.href;
if (fullUrl) pdKeys[8] = encodeURIComponent(fullUrl);

var domain = window.location.host;
if (domain) pdKeys[9] = domain;

// Below may not be accessible from kit
var userAgentString;
if (userAgentString) pdKeys[12] = encodeURIComponent(userAgentString);

var pdRaw = Object.keys(pdKeys).map(function(key){
return key + '=' + pdKeys[key]
}).join('&');

return btoa(pdRaw);
}

function normalizeAndHashEmail(email) {
var SHA256 = require('crypto-js/sha256');

var parts = email.split("@")
var charactersToRemove = ['+', '.']

if (parts[1] != 'gmail.com') {
return email;
}

charactersToRemove.forEach(function(character) {
parts[0] = parts[0].replaceAll(character, '').toLowerCase();
})

return SHA256(parts.join('@'));
}

function normalizeAndHashPhone(phone) {
var SHA256 = require('crypto-js/sha256');

var charactersToRemove = [' ', '-', '(', ')']
charactersToRemove.forEach(function(character) {
phone = phone.replaceAll(character, '');
})
return SHA256(phone);
}

module.exports = initialization;
2 changes: 1 addition & 1 deletion test/end-to-end-testapp/settings.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var SDKsettings = {
apiKey: 'testAPIKey'
partnerId: '1234'
/* fill in SDKsettings with any particular settings or options your sdk requires in order to
initialize, this may be apiKey, projectId, primaryCustomerType, etc. These are passed
into the src/initialization.js file as the
Expand Down
Loading

0 comments on commit 36c72fe

Please sign in to comment.