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.

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.

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as