Paste Search Dynamic
Recent pastes
identity.js
  1. define('core/analytics/identity',[
  2.     'exports',
  3.  
  4.     'core/utils/cookies',
  5.     'core/utils/guid',
  6.     'core/utils/hash',
  7.     'core/utils/fingerprint',
  8. ], function (
  9.     exports,
  10.  
  11.     cookies,
  12.     guid,
  13.     hash,
  14.     fingerprint
  15. ) {
  16.     'use strict';
  17.  
  18.     var initialized = false;
  19.  
  20.     var ImpressionManager = exports.ImpressionManager = function () {
  21.         // Will be updated by calls made from individual app initializations. Defaults to true to be safe
  22.         this.isPrivate = true;
  23.  
  24.         this.impId = guid.generate();
  25.     };
  26.     ImpressionManager.prototype.COOKIE_NAME = '__jid';  // jid is 'jester impression id'
  27.     ImpressionManager.prototype.TTL = 30 * 60 * 1000;  // TTL for the impression cookie is 30 minutes
  28.     ImpressionManager.prototype.init = function (options) {
  29.         // This function should be called upon app initialization, before this manager's values
  30.         // or functions are utilized
  31.  
  32.         this.isPrivate = options && options.isPrivate;
  33.  
  34.         if (!this.isPrivate)
  35.             this.prevImp = cookies.read(this.COOKIE_NAME);
  36.  
  37.         this.persist();
  38.     };
  39.     ImpressionManager.prototype.setImpressionId = function (impId) {
  40.         this.impId = impId;
  41.         this.persist();
  42.     };
  43.     ImpressionManager.prototype.persist = function () {
  44.         // TODO: This is getting wiped if we find out `isPrivate` is actually
  45.         // false later since it is initialized before the session is fetched
  46.         // which can influence `isPrivate`.
  47.         if (this.isPrivate) {
  48.             cookies.erase(this.COOKIE_NAME);
  49.             return;
  50.         }
  51.  
  52.         // Set cookie so the current impression id
  53.         // can be grabbed as the previous impression id in the next invocation.
  54.         // (Expires after 30 minutes of inactivity.)
  55.         cookies.create(this.COOKIE_NAME, this.impId, { expiresIn: this.TTL });
  56.     };
  57.  
  58.  
  59.     var UniqueManager = exports.UniqueManager = function () {
  60.         // Will be updated by calls made from individual app initializations. Defaults to true to be safe
  61.         this.isPrivate = true;
  62.     };
  63.     UniqueManager.prototype.COOKIE_NAME = 'disqus_unique';
  64.     UniqueManager.prototype.TTL = 365 * 24 * 60 * 60 * 1000;  /* one year in ms */
  65.     UniqueManager.prototype.init = function (options) {
  66.         // This function should be called upon app initialization, before this manager's values
  67.         // or functions are utilized
  68.         this.isPrivate = options && options.isPrivate;
  69.  
  70.         if (this.isPrivate) {
  71.             cookies.erase(this.COOKIE_NAME, {
  72.                 domain: window.location.host.split(':')[0],
  73.             });
  74.             return;
  75.         }
  76.  
  77.         // TODO: We are currently generating a new GUID each time we ever
  78.         // initialize with `isPrivate` which is the default behavior.  We
  79.         // could persist the GUID in localStorage so we can use it whenever
  80.         // we have consent and still keep deleting our cookie, or we can
  81.         // decide we don't need to delete our cookie anymore, but we will
  82.         // need to get legal's approval on which approach we are comfortable
  83.         // with.  A version of the latter (storing in localStorage) can be
  84.         // found in an early version of D28850.
  85.         this.value = cookies.read(this.COOKIE_NAME) || guid.generate();
  86.  
  87.         // This needs to be called each time because if we ever
  88.         // change the options, existing cookies will not get updated.
  89.         cookies.create(this.COOKIE_NAME, this.value, {
  90.             domain: window.location.host.split(':')[0],
  91.             expiresIn: this.TTL,
  92.         });
  93.     };
  94.     UniqueManager.prototype.isPersistent = function () {
  95.         // If we were not able to set and read a cookie
  96.         // then the browser does not support cookies.
  97.         return !this.isPrivate && cookies.read(this.COOKIE_NAME) === this.value;
  98.     };
  99.  
  100.     exports.init = function (options, force) {
  101.         if (initialized && !force)
  102.             return;
  103.  
  104.         exports.impression.init(options);
  105.         exports.unique.init(options);
  106.         initialized = true;
  107.     };
  108.  
  109.     exports.reset = function () {
  110.         initialized = false;
  111.  
  112.         exports.impression = new ImpressionManager();
  113.         exports.unique = new UniqueManager();
  114.     };
  115.  
  116.     exports.reset();
  117.  
  118.     /**
  119.      * Returns a consistent ID for this client.
  120.      *
  121.      * Uses either the disqus_unique cookie value or the
  122.      * browser's fingerprint value if cookies are not supported.
  123.      * @returns {string} client id
  124.      */
  125.     exports.clientId = function () {
  126.         var manager = exports.unique;
  127.         var value;
  128.  
  129.         if (manager.isPersistent())
  130.             value = manager.value;
  131.  
  132.         return value || fingerprint.get().toString();
  133.     };
  134.  
  135.     /**
  136.      * Returns a number between 0..99 for a given string identifier.
  137.      *
  138.      * This method of determining a percent using a specific string identifier
  139.      * (such as a forum shortname, username, disqus_unique id, etc.)
  140.      *
  141.      * https://blog.disqus.com/the-paper-plane-proof-our-uplifting-story-of-client-side-javascript-bucketing
  142.      *
  143.      * @param {string} str - String to get calculated into a percent bucket
  144.      * @param {number} precision - Determines the percentage precision
  145.      * @returns {number} Number that is calculated from the hashed input
  146.      */
  147.     exports.getPercentBucketForString = function (str, precision) {
  148.         var max = 100;
  149.         var stringHash = Math.abs(hash.calculate(str));
  150.         if (precision) {
  151.             var divisor = Math.pow(10, precision);
  152.             return (stringHash % (max * divisor)) / divisor;
  153.         }
  154.         return stringHash % max;
  155.     };
  156.  
  157.     /**
  158.      * Calculates the percentile for the client ID.
  159.      *
  160.      * @returns {number} percentile - Number that is calculated from the hashed input
  161.      */
  162.  
  163.     exports.clientPercent = function () {
  164.         return exports.getPercentBucketForString(exports.clientId());
  165.     };
  166. });
  167.  
  168.  
Parsed in 0.024 seconds