Skip to content

Instantly share code, notes, and snippets.

@scottcate
Last active March 28, 2022 01:24
Show Gist options
  • Save scottcate/288e49309f09f1d11b296c5987f58d90 to your computer and use it in GitHub Desktop.
Save scottcate/288e49309f09f1d11b296c5987f58d90 to your computer and use it in GitHub Desktop.
Bookmarklet Template for adding Web Trends tracking
@spboyer
Copy link

spboyer commented Apr 25, 2018

Here is an edit that modifies the above. Asks for the event, and then puts the final url into the clipboard for you to paste for sharing.

javascript:
(
  function () {
    var alias = 'shboyer';
    var event = prompt('Enter value for the event', '');
    var channel = 'twitter';
    
    function copyToClipboard(text) {
      var textarea = document.createElement("textarea");
      textarea.textContent = text;
      textarea.style.position = "fixed";  /* Prevent scrolling to bottom of page in MS Edge.*/
      document.body.appendChild(textarea);
      textarea.select();
      try {
        return document.execCommand("copy");  /* Security exception may be thrown by some browsers.*/
      } catch (ex) {
        console.warn("Copy to clipboard failed.", ex);
        return false;
      } finally {
        document.body.removeChild(textarea);
      }
    }

    //Get current URL
    var baseUrl = document.location.href;
    
    //remove current locale for sharing
    const regex = /microsoft.com\/\w{2}-\w{2}\//g;
    const subst = 'microsoft.com/';
    baseUrl = baseUrl.replace(regex,subst);
    
    //respect or ignore currect query string
    var separator = baseUrl.indexOf('?') > 0 ? '&' : '?';
    
    //respect or ignore hash
    var hash = '';
    var hasHash = baseUrl.indexOf('#');
    if (hasHash != -1) {
      hash = baseUrl.substr(hasHash);
      baseUrl = baseUrl.replace(hash, '');
    }
    
    //build final URL
    var final = baseUrl + separator + 'WT.mc_id=' + event + '-' + channel + '-' + alias + hash;
    
    copyToClipboard(final);
  })();

@scottcate
Copy link
Author

Thank you, Shayne, I updated the original to use your copy to clipboard function!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment