Last active
March 28, 2022 01:24
-
-
Save scottcate/288e49309f09f1d11b296c5987f58d90 to your computer and use it in GitHub Desktop.
Bookmarklet Template for adding Web Trends tracking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Directions | |
// Step 1: Click Gist Raw, then Copy/Paste entire gist into Bookmarklets.org/maker/ | |
// Step 2: Change the first 3 variables: alias, event, docs | |
// Step 3: At the bottom of the Bookmarklets.org page, drag the Bookmarklet Hyperlink to your Browser ToolBar | |
// Step 4: Enjoy | |
var alias = 'scottca'; | |
var event = 'docs'; //docs,build,ignite,blog,think of this as broad category | |
var channel = 'twitter'; //facebook,hackernews,print,think of this as medium being used | |
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; | |
//Copy value to clipboard | |
copyToClipboard(final); | |
//replace URL - does not cause refresh, just changes the URL. | |
window.history.pushState("", "", final); |
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
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.