Deployed to https://asia-northeast1-rickys-blog-v2.cloudfunctions.net/index
Last active
August 30, 2018 00:49
-
-
Save richardkazuomiller/aba769e3e2779bccfad6379d91f9a0a2 to your computer and use it in GitHub Desktop.
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
const https = require('https'); | |
function sendReqToGoogleAnalytics(req,res){ | |
const url = req.query.url; | |
const trackingId = req.query.trackingId; | |
if(!url) { | |
res.status(400); | |
res.send('URL parameter is required'); | |
return; | |
} | |
if(!url.match(/http(s|):\/\//)) { | |
res.status(400); | |
res.send('Invalid URL'); | |
return; | |
} | |
if(!trackingId) { | |
res.status(400); | |
res.send('Tracking ID is required'); | |
return; | |
} | |
let ip = req.headers['x-forwarded-for']; | |
if(ip){ | |
ip = ip.split(',')[0]; | |
} | |
if(!ip){ | |
ip = req.ip; | |
} | |
const gaPath = [ | |
'/collect?v=1&_v=j12&a=151658386&t=pageview&_s=1&dl=/redirect_service?url=',encodeURIComponent(req.query.url), | |
'&dr=&ul=en-us&de=UTF-8&dt=Redirector&sd=24-bit&sr=1920x1200&vp=54x1224&je=1&fl=11.8%20r800&_u=MAC~&cid=',ip, | |
'&uip=',ip, | |
'&tid=',req.query.trackingId, | |
'&z=740710524&dr=',req.headers.referer, | |
].join(''); | |
const options = { | |
host : 'www.google-analytics.com', | |
port : 443, | |
path : gaPath, | |
method : 'GET', | |
headers : { | |
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', | |
'User-Agent': req.headers['user-agent'], | |
'Host' : 'www.google-analytics.com' | |
} | |
}; | |
const gaReq = https.request(options, function(gaRes) { | |
console.log(gaRes.statusCode); | |
res.redirect(req.query.url); | |
}); | |
gaReq.end(); | |
} | |
function index (req, res) { | |
const url = req.query.url; | |
const trackingId = req.query.trackingId; | |
const redirectUrl = url && trackingId | |
? `https://asia-northeast1-rickys-blog-v2.cloudfunctions.net/sendReqToGoogleAnalytics?url=${encodeURIComponent(url)}&trackingId=${encodeURIComponent(trackingId)}` | |
: ''; | |
res.header('content-type', 'text/html'); | |
res.send(html`<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<style> | |
* { | |
box-sizing: border-box; | |
} | |
input[type="text"] { | |
display: block; | |
width: 100%; | |
} | |
</style> | |
</head> | |
<body> | |
<form> | |
<h1>Generate GA Redirect Tracking URL</h1> | |
<label> | |
URL<br/> | |
<input type="text" name="url"/><br/> | |
</label> | |
<label> | |
Tracking ID<br/> | |
<input type="text" name="trackingId" /> <br/> | |
</label> | |
<input type="submit" /> | |
</form> | |
<div> | |
<h2>${redirectUrl ? 'Redirect URL:' : ''}</h3> | |
<p>${redirectUrl}</p></div> | |
</body> | |
</html>`); | |
} | |
function html () { | |
// just a wrapper | |
return String.raw.apply(String, arguments); | |
} | |
module.exports = {sendReqToGoogleAnalytics, index}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://asia-northeast1-rickys-blog-v2.cloudfunctions.net/sendReqToGoogleAnalytics?url=https%3A%2F%2Ftwitter.com%2Fayakix%3Fhoge&trackingId=UA-28914636-2