diff --git a/.gitignore b/.gitignore index 8a57a19..2343d7e 100644 --- a/.gitignore +++ b/.gitignore @@ -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/ diff --git a/package-lock.json b/package-lock.json index 0b6be0e..bb2cfb4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,6 +18,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", @@ -2821,6 +2822,13 @@ "node": "*" } }, + "node_modules/crypto-js": { + "version": "4.2.0", + "resolved": "https://nexus.ops-shared.mparticle.com/repository/npm-group/crypto-js/-/crypto-js-4.2.0.tgz", + "integrity": "sha512-KALDyEYgpY+Rlob/iriUtjV6d5Eq+Y191A5g4UqLAi8CyGP9N1+FdVbkc1SxKc2r4YAYqG8JzO2KGL+AizD70Q==", + "dev": true, + "license": "MIT" + }, "node_modules/crypto-random-string": { "version": "2.0.0", "resolved": "https://nexus.ops-shared.mparticle.com/repository/npm-group/crypto-random-string/-/crypto-random-string-2.0.0.tgz", diff --git a/package.json b/package.json index 5b18d4b..0675828 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/initialization.js b/src/initialization.js index e107aac..be817d4 100644 --- a/src/initialization.js +++ b/src/initialization.js @@ -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. @@ -17,22 +21,24 @@ var initialization = { Generally, our integrations create script tags and append them to the . 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) @@ -40,4 +46,61 @@ var initialization = { } }; + +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;