Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Preserve other params when stripping Google Analytics tracking.

  • Loading branch information...
commit 10b42e49fb94fbfc8c046abdc273c460b2cef66b 1 parent 5a7db5c
@jasoncodes authored
View
3  _layouts/default.html
@@ -50,7 +50,8 @@
_gaq.push(['_setAccount', '{{ site.google_analytics_id }}']);
_gaq.push(['_trackPageview']);
_gaq.push(function() {
- if (history.replaceState) history.replaceState(null, '', location.pathname + location.hash);
+ var newPath = location.pathname + location.search.replace(/[?&]utm_[^?&]+/g, "").replace(/^&/, "?") + location.hash;
+ if (history.replaceState) history.replaceState(null, '', newPath);
});
(function() {
if (location.host.match(/(\.local|^localhost)(:\d+)?/)) return;
View
9 _posts/clean-google-analytics-tracking-urls.markdown
@@ -3,7 +3,7 @@ layout: post
title: Clean Google Analytics tracking data from your URLs
short: ga
date: 2011-01-09
-updated: 2011-01-26
+updated: 2013-01-07
---
FeedBurner has a feature which integrates with Google Analytics to tell you how many of your hits come via feed readers. This is rather useful because feed readers typically don't pass any useful referrer information (especially desktop clients). This tracking can be enabled by ticking the "[Track clicks as a traffic source in Google Analytics](http://www.google.com/support/feedburner/bin/answer.py?hl=en&answer=165769)" option in your FeedBurner account.
@@ -23,7 +23,8 @@ var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_trackPageview']);
_gaq.push(function() {
- if (history.replaceState) history.replaceState(null, '', location.pathname + location.hash);
+ var newPath = location.pathname + location.search.replace(/[?&]utm_[^?&]+/g, "").replace(/^&/, "?") + location.hash;
+ if (history.replaceState) history.replaceState(null, '', newPath);
});
(function() {
@@ -33,4 +34,6 @@ _gaq.push(function() {
})();
{% endhighlight %}
-**Update:** I originally called `replaceState` with just `location.pathname` but that resulted in the removal of anchors within a page (e.g. links to comments). The code has been updated to `location.pathname + location.hash` to keep anchors. e.g. `/foo/bar?utm_medium=example#comment-42` will now be replaced with `/foo/bar#comment-42`.
+**Update 2011-01-26:** I originally called `replaceState` with just `location.pathname` but that resulted in the removal of anchors within a page (e.g. links to comments). The code has been updated to `location.pathname + location.hash` to keep anchors. e.g. `/foo/bar?utm_medium=example#comment-42` will now be replaced with `/foo/bar#comment-42`.
+
+**Update 2013-01-07:** Thanks [Henrik Nyh](http://henrik.nyh.se/) for a [patch](#comment-758637324) to preserve parameters other than those used by Google Analytics.
Please sign in to comment.
Something went wrong with that request. Please try again.