Add analytics to expandos

Categorize as selftext or external
Also, modify/improve domain tracking
This commit is contained in:
Keith Mitchell
2011-08-15 16:29:16 -07:00
parent e175e5e132
commit 86eef859ec
4 changed files with 60 additions and 10 deletions

View File

@@ -1,28 +1,59 @@
/* Analytics, such as event tracking for google analytics */
$(function() {
/* _trackEvent takes 2-3 parameters, which act like "tags". For
our purposes:
* category: Type of event (outbound link, embed video, etc.)
* action: domain, for aggregating data by domain. Will be
"self.<subreddit>" for subreddits
* label: Full URL of thing
*/
function recordOutboundLink(link) {
/* category/action/label are essentially "tags" used by
google analytics */
var category = "outbound link";
var action = link.attr("domain");
var label = link.attr("srcurl") || link.attr("href");
var action = parse_domain(label);
/* Find the parent link <div> for info on promoted/self/etc */
var link_entry = link.thing();
if (link_entry.hasClass("selflink")){
if (link_entry.hasClass("self")) {
category = "internal link";
}
if (link_entry.hasClass("promotedlink")){
if (link_entry.hasClass("promotedlink")) {
category += " promoted";
}
_gaq.push(['_trackEvent', category, action, label]);
}
$("body").delegate("div.link .entry .title a.title, div.link a.thumbnail",
function recordExpando() {
/* Track self-post or embedded item expanded */
var expando = $(this);
if (expando.hasClass("tracked")) {
return;
}
var thing = expando.thing();
var link = thing.find("a.title");
var category = "embed";
if (expando.hasClass("selftext")) {
category += " self";
} else if (expando.hasClass("video")) {
category += " external";
}
var label = link.attr("srcurl") || link.attr("href");
var action = parse_domain(label);
_gaq.push(['_trackEvent', category, action, label]);
expando.addClass("tracked");
}
$("body").delegate("a.title, a.thumbnail, a.reddit-link-title, .self a.comments",
"mouseup", function(e) {
switch (e.which){
/* Record left and middle clicks */
@@ -38,5 +69,7 @@ $(function() {
}
});
$("body").delegate("div.expando-button", "click", recordExpando);
});

View File

@@ -1417,3 +1417,23 @@ function save_href(link) {
}
return link;
}
function pure_domain(url) {
var domain = url.match(/:\/\/([^/]+)/)
if (domain) {
domain = domain[1].replace(/^www\./, '');
}
return domain;
}
function parse_domain(url) {
var domain = pure_domain(url);
if (!domain) {
/* Internal link? Get the SR name, if there is one */
var reddit = url.match(/\/r\/([^/]+)/)
if (reddit) {
domain = "self." + reddit[1].toLowerCase();
}
}
return domain;
}

View File

@@ -44,9 +44,6 @@
%if not (getattr(thing, "trial_mode", None) and thing.is_self):
href="${thing.href_url}"
%endif
%if thing.domain:
domain="${thing.domain}"
%endif
%if thing.nofollow:
rel="nofollow"
%endif

View File

@@ -58,7 +58,7 @@ ${self.RenderPrintable()}
cls = thing.lookups[0].__class__.__name__.lower()
if getattr(thing, "is_self", False):
selflink = "selflink"
selflink = "self"
else:
selflink = ""