Skip to content

Instantly share code, notes, and snippets.

@ayakix
Last active October 25, 2018 01:37
Show Gist options
  • Save ayakix/a77f532a632b0cdb0993eb47ce92d708 to your computer and use it in GitHub Desktop.
Save ayakix/a77f532a632b0cdb0993eb47ce92d708 to your computer and use it in GitHub Desktop.
AWS GWのモック機能とGoogle Analyticsを使って任意のWebページのアクセスログを簡単に取るやつ
<!--
Ex.)
Target url: https://google.co.jp?hoge=abc&fuga=def
Campaign url: https://xxxxx.execute-api.xxxx.amazonaws.com/xxx?url=https%3A%2F%2Fwww.google.co.jp%3Fhoge%3Dabc%26fuga%3Ddef&ua=UA-xxxxxx-x&utm_source=source&utm_medium=medium&utm_campaign=campaign&utm_term=term&utm_content=content
-->
<!DOCTYPE html>
<head>
<meta charset="utf-8">
</head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=$input.params('ua')"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', "$input.params('ua')");
// リダイレクト(リファラの持ち越しとかには対応してない)
location.href = decodeURIComponent("$input.params('url')");
</script>
</html>
@ayakix
Copy link
Author

ayakix commented Aug 29, 2018

2018-08-29 13 58 10

@ayakix
Copy link
Author

ayakix commented Oct 25, 2018

<!DOCTYPE html>
<head>
  <meta charset="utf-8">
</head>

<script>
  window.query = location.search.replace(/^\?/, '').split('&').reduce(function (acc, p) {
    var p = p.split('=');
    acc[p[0]] = p[1];
    return acc;
  }, {})
</script>

<script>
  var script = document.createElement('script');
  script.setAttribute('src', 'https://www.googletagmanager.com/gtag/js?id=' + query.ua);
  document.head.appendChild(script);

  window.dataLayer = window.dataLayer || [];
  function gtag(){ dataLayer.push(arguments); }
  gtag('js', new Date());
  gtag('config', query.ua);

  var i = setInterval(function() {
    if (window.dataLayer.filter(function(e) { return e.event === 'gtm.load' }).length > 0) {
      clearInterval(i);
      location.href = decodeURIComponent(query.url);
    }
  }, 500)
</script>
</html>

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