Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: initialization and ID5 ID access/storage #1

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
node_modules/
test/test-bundle.js
.DS_Store
test/end-to-end-testapp/compilation.js
test/end-to-end-testapp/build/compilation.js
dist/
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@semantic-release/exec": "^5.0.0",
"@semantic-release/git": "^9.0.0",
"chai": "^4.2.0",
"crypto-js": "^4.2.0",
"eslint": "^7.25.0",
"eslint-config-prettier": "9.1.0",
"karma": "^5.1.0",
Expand Down
95 changes: 79 additions & 16 deletions src/initialization.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// import SHA256 from 'crypto-js/sha256';

var initialization = {
name: 'ID5',

//To-Do: add Module id after establishing in QA
moduleId: '',
/* ****** Fill out initForwarder to load your SDK ******
Note that not all arguments may apply to your SDK initialization.
Expand All @@ -17,27 +21,86 @@ var initialization = {
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://www.clientscript.com/static/clientSDK.js'; // <---- Update this to be your script
// (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(clientScript);
// clientScript.onload = function() {
// if (clientSDKObject && eventQueue.length > 0) {
// // Process any events that may have been queued up while forwarder was being initialized.
// for (var i = 0; i < eventQueue.length; i++) {
// processEvent(eventQueue[i]);
// }
// // now that each queued event is processed, we empty the eventQueue
// eventQueue = [];
// }
// clientSDKObject.initialize(forwarderSettings.apiKey);
// };
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)
});
};
} else {
// For testing, you should fill out this section in order to ensure any required initialization calls are made,
// clientSDKObject.initialize(forwarderSettings.apiKey)
}
}
};


function buildPartnerData(userIdentities) {
var SHA256 = require('crypto-js/sha256');

// To-Do: finalize which PD values we are utilizing
var email = userIdentities.userIdentities['email'];
var cleansedEmail = normalizeEmail(email);
var hashedEmail = SHA256(cleansedEmail);

var phone = userIdentities.userIdentities['mobile_number'];
var cleansedPhone = normalizePhone(phone);
var hashedPhoneNumber = SHA256(cleansedPhone);

var fullUrl = window.location.href;
var deviceIPv4;
var userAgentString;
var idfv;

var pdKeys = {
1: hashedEmail,
2: hashedPhoneNumber,
8: encodeURIComponent(fullUrl),
10: encodeURIComponent(deviceIPv4),
12: encodeURIComponent(userAgentString),
14: encodeURIComponent(idfv),
}

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

return btoa(pdRaw);
}

function normalizeEmail(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 parts.join('@');
}

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

module.exports = initialization;
Loading