Fri Feb 15 2019
Copied to clipboard! Copy reply
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
/*
*********************************************************************
SNAPCHAT PAGE VIEW PIXEL
	purpose: tracking success of snapchat ads
	other names: scevent, sc-static.net
	owners: dion, caitlin
	ticket: WS-2913
	added: sep 14, 2018
	updated: oct 3, 2018
*********************************************************************
*/

(function(){

	// *********************************************************************
	// GET EMAIL FROM COOKIE - ONLY USE AFTER HASHING
	// *********************************************************************
	var email = (document.cookie.match(/(email|curEmailIdsc)=([^;]+)/i)||['','',''])[2].trim().toLowerCase();
	var hashedEmail;
	hashEmail(email).then(hash => {
		hashedEmail = hash;

		run_snapchat_pixel(email ? hashedEmail : null);
	});



	// *********************************************************************
	// sha256 hash of email (originally from WS-2454)
	// *********************************************************************	

	function hashEmail(email) {
		// Feature checks for Uint8Array, Promises, and crypto.subtle
		var _crypto = window.crypto || window.msCrypto;
		if(!(typeof _crypto == 'object' && !!_crypto.subtle && typeof Uint8Array == 'function' && typeof Promise == 'function')){
			return null;
		}

		function hex(buffer){
			var hexCodes = [];
			var view = new DataView(buffer);
			for (var i=0; i<view.byteLength; i+=4){
				// Using getUint32 reduces the number of iterations needed (we process 4 bytes each time)
				var value = view.getUint32(i);
				// toString(16) will give the hex representation of the number without padding
				var stringValue = value.toString(16);
				// We use concatenation and slice for padding
				var padding = '00000000';
				var paddedValue = (padding + stringValue).slice(-padding.length);
				hexCodes.push(paddedValue);
			}

			// Join all the hex strings into one
			return hexCodes.join('');
		}

		// We transform the string into an arraybuffer.
		var buffer = new TextEncoder("utf-8").encode(email);
		return _crypto.subtle.digest("SHA-256", buffer).then(function (hash) {
			return hex(hash);
		});
	}





	// *********************************************************************
	// SNAPCHAT PIXEL
	// *********************************************************************

	function run_snapchat_pixel(hashedEmail){

		(function(win, doc, sdk_url) {
			if (win.snaptr) return;
			var tr = win.snaptr = function() {
				tr.handleRequest ? tr.handleRequest.apply(tr, arguments) : tr.queue.push(arguments);
			};
			tr.queue = [];
			var s = 'script';
			var new_script_section = doc.createElement(s);
			new_script_section.async = !0;
			new_script_section.src = sdk_url;
			var insert_pos = doc.getElementsByTagName(s)[0];
			insert_pos.parentNode.insertBefore(new_script_section, insert_pos);
		})(window, document, 'https://sc-static.net/scevent.min.js');
		snaptr('init', 'fea5dcf0-9dce-497f-9da1-39d657056fbd', hashedEmail ? {
			'user_hashed_email': hashedEmail
		} : {});


		snaptr('track', 'PAGE_VIEW');
	}

})();