|
JavascriptClientApiDocumentation
Documentation of the bit.ly Javascript Client API
See also ApiDocumentation for the bit.ly REST API Sections Informationbit.ly provides a javascript client library that makes it easy for developers to make calls to the bit.ly REST API. Authentication and Basic InstructionsThe bit.ly javascript client library is available at: http://bit.ly/javascript-api.js?version=latest&login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07 To use the bit.ly javascript client library, you must pass your login and apiKey in the URL instead of the sample ones above. If you do not have an apiKey, sign up for a bitly account and go to your Account page to get your apiKey. To include the bit.ly javascript client library on one of your pages, add this inside the <head> tag: <script type="text/javascript" charset="utf-8" src="http://bit.ly/javascript-api.js?version=latest&login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07"></script> When you include the bit.ly javascript client library, a new instance of the BitlyApiClient class is instantiated and assigned to the global variable BitlyClient BitlyCB global namespaceUse the BitlyCB namespace to define callback methods for API calls. See the BitlyApiClient.call method below for an example of defining a callback in the BitlyCB namespace. All calls made to the bit.ly REST API return data using JSONP + callback methods so that you can make calls to bit.ly and use data from bit.ly from an external domain. BitlyApiClient classUse an instance of the BitlyApiClient class to make calls to the bit.ly API using your login and apiKey. Instance MethodsnewCreates a new instance of BitlyApiClient class. Generally, you should not ever need to create your own instance of the BitlyApiClient class. Instead, you should use the BitlyClient global instance of the BitlyApiClient class that is instantiated when you include the bit.ly javascript client library. Parameters
bit.ly login bit.ly apiKey Version of the bit.ly REST API to call. callLow level method for calling bit.ly REST API. Parameters
Name of the bit.ly REST API method to call. Parameters of teh bit.ly REST API method being called. Method to be called when response is returned from bit.ly REST API. Define this method within the !BitlyCB global namespace. Example: BitlyCB.alertResponse = function(data) {
var s = '';
var first_result;
// Results are keyed by longUrl, so we need to grab the first one.
for (var r in data.results) {
first_result = data.results[r]; break;
}
for (var key in first_result) {
s += key + ":" + first_result[key].toString() + "\n";
}
alert(s);
}
BitlyClient.call('shorten', {'longUrl': 'http://google.com'}, 'BitlyCB.alertResponse');shortenShorten a long URL. Parameters
Long URL to shorten. Method to be called when response is returned from bit.ly REST API. Define this method within the !BitlyCB global namespace. Examples: BitlyCB.shortenResponse = function(data) {
var s = '';
var first_result;
// Results are keyed by longUrl, so we need to grab the first one.
for (var r in data.results) {
first_result = data.results[r]; break;
}
for (var key in first_result) {
s += key + ":" + first_result[key].toString() + "\n";
}
alert(s);
}
BitlyClient.shorten('http://google.com', 'BitlyCB.shortenResponse');expandExpand a bit.ly URL. Parameters
Short URL to expand. Method to be called when response is returned from bit.ly REST API. Define this method within the !BitlyCB global namespace. Examples: BitlyCB.expandResponse = function(data) {
var s = '';
var first_result;
// Results are keyed by longUrl, so we need to grab the first one.
for (var r in data.results) {
first_result = data.results[r]; break;
}
for (var key in first_result) {
s += key + ":" + first_result[key].toString() + "\n";
}
alert(s);
}
BitlyClient.expand('http://bit.ly/3j4ir4', 'BitlyCB.expandResponse');
BitlyClient.expand('http://bit.ly/1YKMfY', 'BitlyCB.expandResponse');infoGet info about a bit.ly URL. Parameters
A single bit.ly URL Method to be called when response is returned from bit.ly REST API. Define this method within the !BitlyCB global namespace. Examples: BitlyCB.infoResponse = function(data) {
var s = '';
var result;
for (var r in data.results) {
result = data.results[r];
for (var key in result) {
s += key + ":" + result[key].toString() + "\n";
}
}
alert(s);
}
BitlyClient.info('3j4ir4', 'BitlyCB.infoResponse');statsGet clicks and referrer data about a bit.ly URL. Parameters
Either a single bit.ly URL or a single bit.ly hash. Method to be called when response is returned from bit.ly REST API. Define this method within the !BitlyCB global namespace. Examples: BitlyCB.statsResponse = function(data) {
var s = '';
var result = data.results;
for (var key in result) {
s += key + ":" + result[key].toString() + "\n";
}
alert(s);
}
BitlyClient.stats('3j4ir4', 'BitlyCB.statsResponse');addPageLoadEventPerform a function once the DOM is done rendering. You would use this if you want to safely manipulate the DOM, eg, append stats to all bit.ly link elements. Parameters
Either a reference to a function or an anonymous function to execute once the page has loaded. Example: BitlyClient.addPageLoadEvent(function(){
alert("Page is loaded. It's now safe to manipulate the DOM.");
});extractBitlyHashUtility method for ensuring you have a bit.ly hash, given a bit.ly URL or hash. Parameters
bit.ly URL (or hash) you want to convert into a simple bit.ly hash. Example: alert(BitlyClient.extractBitlyHash("http://bit.ly/3j4ir4"));createElementUtility method for creating a DOM node to insert into the page. Parameters
Type of DOM node to create, eg: 'a' or 'div' Object containing key/value pairs specifying attributes you would like to set on the DOM node, eg: {'class': 'bitly_link', 'text': 'http://bit.ly/3j4ir4', 'href': 'http://bit.ly/3j4ir4'} NOTE Use the key 'text' to set the innerText of the node. Example: var a = BitlyClient.createElement('a', {'href': 'http://bit.ly', 'text': 'http://bit.ly'});
document.body.appendChild(a);setAttributeUtility method for setting an attribute on a DOM node. Parameters
DOM node to manipulate, eg. the result of a document.getElementById call. Name of the attribute to set, eg. 'class' or 'id' or 'href' Value to set the attribute to, eg. 'http://bit.ly' Example: var a = BitlyClient.createElement('a', {'text': 'http://bit.ly'});
a.setAttribute('href', 'http://bit.ly');
document.body.appendChild(a);loadModulesLoad one of the bit.ly javascript client library modules. These modules provide very easy ways to do common tasks, like append stats to each bit.ly URL on a page or insert a traffic / referrers data table. Parameters
Array containing the names of modules you wish to load. Method to be called when all modules you requested are done loading. Define this method within the !BitlyCB global namespace. NOTE: Because modules involve DOM manipulation, this callback method also waits for the page to load before firing. Example: BitlyCB.modulesReady = function() {
BitlyClient.addStatsToBitlyLinks(); // method defined in BitlyClient stats module
}
BitlyClient.loadModules(['stats'], 'BitlyCB.modulesReady');Bitly Client ModulesBitlyClient Modules provide a convenient methods for executing common tasks. Modules are loaded dynamically, so you only have to load those that you want to use. stats moduleThe stats module adds the following Instance Methods to BitlyClient. addStatsToBitlyLinksAppend the number of clicks to every bit.ly link on a page. You can use CSS to show / hide the data as you want. The HTML appended to each bit.ly link element looks like this: <span class="bitly_stats" classname="bitly_stats"> <span class="bitly_left_bracket" classname="bitly_left_bracket"> [ </span> <a target="_blank" href="http://bit.ly/info/31IqMl" class="bitly_stats_info_link" classname="bitly_stats_info_link"> <span class="bitly_user_clicks" classname="bitly_user_clicks"></span> <span class="bitly_clicks" classname="bitly_clicks">493 clicks</span> </a> <span class="bitly_right_bracket" classname="bitly_right_bracket"> ] </span> </span> Parameters None Example: BitlyCB.modulesReady = function() {
BitlyClient.addStatsToBitlyLinks(); // method defined in BitlyClient stats module
}
BitlyClient.loadModules(['stats'], 'BitlyCB.modulesReady');appendStatsTableGiven url, insert a table showing referring URLs and the number of clicks on each referrer. You can specify as an argument either a bit.ly url or a long url, such as the url of the page currently being viewed (document.URL). Parameters
A javascript object containing either a longUrl or a bit.ly url, eg either {'longUrl': 'http://google.com'} OR {'shortUrl': 'http://bit.ly/3j4ir4'} DOM id of the element that should contain the html table showing referrer data, eg: "results_div" function modulesReady() {
var l = "http://google.com"; // document.URL
BitlyClient.appendStatsTable({"longUrl": l}, "result");
}
BitlyClient.loadModules(['stats'], 'modulesReady');DiscussionPlease visit our Google Group for discussion of projects and technical issues. http://groups.google.com/group/bitly-api |
Sign in to add a comment
Does the Javascript client also take care of the rate limit of 5 concurrent connections? That would be very helpful.
Is this working as expected? The code here and the code from the link are different.
Is it possible to get the urls created using JavaScript? API to be listed in the account?