sha1
sha1

Reputation: 555

Google Analytics for bookmarklets

I made a bookmarklet and I would like to use Google Analytics to track it's usage. How would I do that? I found an old post by Remy Sharp that does what I would like but I'm unsure if its outdated. http://remysharp.com/2009/02/27/analytics-for-bookmarklets-injected-scripts/

Upvotes: 2

Views: 653

Answers (2)

sha1
sha1

Reputation: 555

I ended up doing the following

(function() {
 //loading ga.js - not greatest idea, but can't rely on page having ga.js already
    var ga = document.createElement('script');
    ga.type = 'text/javascript';
    ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(ga, s);

 //made a "cool_name" to be able to sort in GA and added domain URL/URI      
    var url = "/cool_name/" + location.host + location.pathname;

 //used "anotherGA" as a 'namespace' to not screw up the real GA for that domain
    _gaq.push(['anotherGA._setAccount', 'UA-XXXXXX-XX']);
    _gaq.push(['anotherGA._setDomainName']);//not sure if i need this
    _gaq.push(['anotherGA._trackPageview', url]);
})();

see: https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApi_gaq (towards the bottom of the page)

To be honest, I don't know if this is an anti-pattern and a misuse of the API, but it seems to work.

Upvotes: 0

CrayonViolent
CrayonViolent

Reputation: 32532

You may need to update the script to point to GA's latest request URL format, but the principle is still okay.

Upvotes: 1

Related Questions