Last active
September 18, 2018 23:46
-
-
Save Megabytemb/fc8f53b82d2d27939840d94713830aca to your computer and use it in GitHub Desktop.
How to use Google Analytics in Apps Script Webapps
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
function doGet(e) { | |
var salt = "Djdids((cx"; | |
var page = HtmlService | |
.createTemplateFromFile('index') | |
page.data = { | |
AnalyticsUserID: getHash(salt + Session.getActiveUser().getEmail()) | |
}; | |
page.params = { | |
utm_source: e.parameter["utm_source"], | |
utm_medium: e.parameter["utm_medium"], | |
utm_campaign: e.parameter["utm_campaign"] | |
}; | |
return page.evaluate() | |
.addMetaTag('viewport', 'width=device-width, initial-scale=1'); | |
} | |
function getHash(message){ | |
message = message; | |
var signature = Utilities.computeDigest( | |
Utilities.DigestAlgorithm.SHA_256, | |
message, | |
Utilities.Charset.US_ASCII); | |
var signatureStr = ''; | |
for (i = 0; i < signature.length; i++) { | |
var byte = signature[i]; | |
if (byte < 0) | |
byte += 256; | |
var byteStr = byte.toString(16); | |
// Ensure we have 2 chars in our byte, pad with 0 | |
if (byteStr.length == 1) byteStr = '0'+byteStr; | |
signatureStr += byteStr; | |
} | |
return signatureStr; | |
} |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<!-- Google Analytics --> | |
<script> | |
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ | |
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), | |
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) | |
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); | |
var GA_LOCAL_STORAGE_KEY = 'ga:clientId'; | |
if (window.localStorage) { | |
ga('create', '<Analytics ID>', { | |
'storage': 'none', | |
'clientId': localStorage.getItem(GA_LOCAL_STORAGE_KEY), | |
'userId': '<?!= data.AnalyticsUserID ?>' | |
}); | |
ga(function(tracker) { | |
localStorage.setItem(GA_LOCAL_STORAGE_KEY, tracker.get('clientId')); | |
}); | |
} | |
else { | |
ga('create', '<Analytics ID>', 'auto', { | |
'userId': '<?!= data.AnalyticsUserID ?>' | |
}); | |
} | |
<? if (params.utm_source) { ?> | |
console.log('<?!= params.utm_source ?>') | |
ga('set', 'campaignSource', '<?!= params.utm_source ?>'); | |
<? } ?> | |
<? if (params.utm_medium) { ?> | |
console.log('<?!= params.utm_medium ?>') | |
ga('set', 'campaignMedium', '<?!= params.utm_medium ?>'); | |
<? } ?> | |
<? if (params.utm_campaign) { ?> | |
console.log('<?!= params.utm_campaign ?>') | |
ga('set', 'campaignName', '<?!= params.utm_campaign ?>'); | |
<? } ?> | |
ga('send', 'pageview'); | |
</script> | |
<!-- End Google Analytics --> | |
</head> | |
<body> | |
Content!!! | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment