devxlogo

Campaign Tracking with Bit.ly API integration

Campaign Tracking with Bit.ly API integration

Although Bit.ly’s URL shortening service is frequently used in conjunction with Twitter, its use is not limited to that. Bit.ly can be used to track any social network or e-mail marketing campaign, as it tracks information about the number of clicks and your audience. They also offer an API, so you can connect it with your application and automate the analysis process. Continue reading to learn how to integrate the Bit.ly API with your application.

API Overview

Bit.ly API uses two types of authentication. If you just want to shorten links on behalf of a single user or website, you can call the API shorten method using the generic OAuth token. On the other hand, if you are working with multiple users or want to access user-level data (Bit.ly history, analytics, etc.), you will need to register an app and use the usual OAuth authentication flow.

API responses are in JSON format by default. However, XML format is also available. When a request is successful, the HTTP status code will be 200. HTTP status codes that indicate errors are:

  • 403 — Rate limited
  • 503 — Service temporary unavailable
  • 404 — Page not found
  • 400 — Other invalid requests

A sample response would look like this:

// Successful response{ "status_code": 200, "status_txt": "OK", "data" : ... }// Unsuccessful response{ "status_code": 403, "status_txt": "RATE_LIMIT_EXCEEDED", "data" : null }

The Bit.ly API has three endpoint URLs:

To make the integration easier, there are many Bit.ly API libraries for a variety of programming languages, that can be downloaded from here.

See also  Custom Java Web Development - The Heartbeat of Modern Web Development

Link Shortening

As mentioned above, this is the simplest (but also very common) Bit.ly API use-case. It does not require the usual OAuth authentication flow, but just a generic OAuth access token, which can be created here.

Once you have done that, you will only have to request the following URL using the HTTP GET method:

https://api-ssl.bitly.com/v3/shorten?access_token=ACCESS_TOKEN&longUrl=URL_TO_SHORTEN

Note that the https://api-ssl.bitly.com is the API URL, v3/shorten is the method that we are calling, and the parameters are passed as GET parameters. Also, the URL you are shortening should be encoded. A sample response would look like this:

{                "status_code": 200,                "status_txt": "OK",                "data": {                                 "long_url": "http://google.com/",                                 "url": "http://bit.ly/1GBL67S",                                 "hash": "1GBL67S",                                "global_hash": "LmvF",                                 "new_hash": 1                    } }

OAuth Authentication

OAuth authentication is a bit more difficult. It is used when you want to work with multiple users or access user-level data. After registering your app, the first step is to redirect a user to the following URL:

https://bitly.com/oauth/authorize?client_id=ENTER_CLIENT_ID&redirect_uri=ENTER_REDIRECT_URI

Client ID is obtained during the app registration, while the redirect URI is the location of the PHP script that will exchange a request token for an access token. So, once the user is redirected to the URL above, he will have to confirm that he allows your app to access his data. Once that is done, he will be redirected to the redirect URI. Bit.ly would send the request token (code) to this URI and then the request token will be exchanged for an access token. If everything goes fine, you can call API methods using the obtained access token.

The redirect URI PHP script would contain the code similar to this:

$value) { $params_string .= $key.'='.$value.'&'; }  		rtrim($params_string,'&');  	try {		$ch = curl_init();		curl_setopt($ch,CURLOPT_URL, $uri);		curl_setopt($ch,CURLOPT_POST, count($params));		curl_setopt($ch,CURLOPT_POSTFIELDS, $params_string);		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);	    	$output = curl_exec($ch);  	} catch (Exception $e) {  	}	// Returns a URL encoded string in the format of "access_token=%s&login=%s&apiKey=%s"		// parse_str() creates variables $access_token and $login	parse_str($output);	// The access token for specified user	echo $access_token;	// The end-user's Bitly username	echo $login;	// Call API methods here	// Store access token in session for later use ?> 

Next Steps

It is good practice to save the access token in a session, so you don’t have to do the exchange each time you want to make an API call.

Also, read about other available API methods here. Calling them is the same as calling the v3/shorten method.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist