Skip to content
Browse files

auto-instrument button with trackSocial (google analytics)

  • Loading branch information...
1 parent 560b3a5 commit fd9e14180928a0cd3cc9d529747df38d2e67b10e @igrigorik committed
Showing with 12 additions and 2 deletions.
  1. +2 −2 hnbutton/button.html
  2. +10 −0 static/hn.js
View
4 hnbutton/button.html
@@ -18,9 +18,9 @@
<body>
<div>
{{if .Id}}
- <a href="http://news.ycombinator.com/item?id={{.Id}}" target="_blank">
+ <a onclick="parent.postMessage('vote','*')" href="http://news.ycombinator.com/item?id={{.Id}}" target="_blank">
{{else}}
- <a href="http://news.ycombinator.com/submitlink?u={{.Url}}&amp;t={{.Title}}" target="_blank">
+ <a onclick="parent.postMessage('submit','*')" href="http://news.ycombinator.com/submitlink?u={{.Url}}&amp;t={{.Title}}" target="_blank">
{{end}}
<span class="hn_bt">
<table cellspacing="0" cellpadding="0">
View
10 static/hn.js
@@ -1,4 +1,14 @@
(function(d){
+ var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
+ var eventer = window[eventMethod];
+ var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message";
+
+ eventer(messageEvent,function(e) {
+ if (typeof(_gaq) != "undefined") {
+ _gaq.push(['_trackSocial', 'hacker news', e.data])
+ }
+ },false);
+
var hnAnchorElements = document.getElementsByClassName("hn-share-button");
for(var e = hnAnchorElements.length - 1; e >= 0; e--) {
title = hnAnchorElements[e].getAttribute("data-title") || window.document.title;

0 comments on commit fd9e141

Please sign in to comment.
Something went wrong with that request. Please try again.