Essential building blocks for marketing automation and growth hacking.

Automating Link Tracking for Digital Marketers

To be a successful growth hacker, or a marketer in the digital world, you need to be tech-savvy. Learn how to leverage the power of APIs for without programming. This is how you automate link tracking using the Bitly API.

Stefan Pettersson

--

Link tracking is essential to evaluate your marketing effort. When you are not in control of the destination where you drive traffic, it is not always you have access to analytics tools. With link tracking, you can measure how many have clicked on a particular link.

Typical scenarios are when driving traffic to a social media account, client’s website, Facebook post, App Store, etc. A link in bio is common in influencer marketing on Instagram, or in the description of a YouTube video.

Often you need to create multiple links, either to distinguish between different sources or when you have multiple destinations. Doing this manually isn’t only time-consuming, it will make it impossible to do marketing at scale.

An influencer marketing campaign or affiliate program may easily result in several hundred links that must be created as well as tracked.

Compared to marketers that don’t make use of the available technology, you will not only be a thousand times more efficient — it will allow you to be creative and do things that they can’t even imagine is possible.

By leveraging automation in marketing, scale is not a factor.

You don’t need to be a programmer to automate the use of tools and third-party services. This is how to use Bitly’s API to automate link creation and statistics retrieval.

Using the API without being a programmer

You can leverage the power of Bitly’s API without being a programmer. You will not need to write any code; instead, we will invoke methods in the API by doing calls from the command-line.

If you want to know more about how to use an API without programming, please see the following article:

In this article, you’ll get concise instructions to be able to execute each step. You need to be comfortable in installing an application, dealing with files and basic navigation using the Terminal. At the end of this article, there’s a section with suggestions for further reading.

Preparation

This guide assumes you are using macOS. We will use the Terminal to run commands, curl to make API calls and the command-line tool jq to convert the response into comma-separated values that can be imported into a spreadsheet.

The terminal and the curl command already comes with your Mac. jq you need to download and install.

You do also need to have registered an account on Bitly (free).

Step 1: Get your Access Token

To be able to use the Bitly API you first need an access token. When you make an API call, the access token does identify your account and enable you to get data from, and modify, your account. You only need to get an access token once (unless you later invalidates it).

The easiest way to get your access token is by logging in to your Bitly account and click the menu button, select Edit Profile, then Generic Access Token. Confirm with your password, and you’ll get the access token.

An alternative way to get your access token is to use the terminal command-line and run (edit and replace with your own Bitly account email/password):

curl -u "myemail@domain.com:xyz123mypassword" -X POST "https://api-ssl.bitly.com/oauth/access_token"

An access token is a string made out of digits and letters a to f. The access token will be used in all requests to the API. In this article the token aad7ecf6cd10e8f072651f00cf90e2bc983a4a974 is used, however, you must substitute it with your own access token through-out all examples.

Step 2: Get your Group GUID

In Bitly there are groups. Even if you don’t use groups and only have a basic account, a group must be specified in all API requests with a Group GUID. You only need to do this once.

By running the following command in your terminal, you call the groups method of the Bitly API to retrieve which groups your account belong to (typically just one).

curl -s -X GET https://api-ssl.bitly.com/v4/groups -H 'Authorization: Bearer ad7ecf6cd10e8f072651f00cf90e2bc983a4a974'

Please note that you need to substitute the access token after “Bearer” with your own.

The response is in JSON format. When everything is crammed in on a single line, it may be hard to read. By “piping” the output of the curl command to jq, we make it easier to read (piping = one command is sending its output as input to another command). This is accomplished by appending | jq . at the end of the previous command.

curl -s -X GET https://api-ssl.bitly.com/v4/groups -H 'Authorization: Bearer ad7ecf6cd10e8f072651f00cf90e2bc983a4a974' | jq .

The response may look like this:

{
"groups": [{
"created": "2009–09–17T09:09:06+0000",
"modified": "2016–11–12T00:46:11+0000",
"bsds": [],
"guid": "B99h99pjymR",
"organization_guid": "O98h9l8bkzN",
"name": "stpe",
"is_active": true,
"role": "org-admin",
"references": {
"organization": "https://api-ssl.bitly.com/v4/organizations/O98h9l8bkzN"
}
}]
}

The value we need is the guid, in this example B99h99pjymR. In order to create bitlinks using the API we need to use this id to specify group.

Step 3: Create bitlinks

To get a shortened bitlink for an URL we use the createBitlink API method. The data we need to send to the API is the group_guid we got in the previous step and the long_url which is the link you want to shorten.

This is how to shorten one link into a bitlink (note: the backslash is included, it does allow us to put the command on multiple lines):

The API responds with the following, where link is the shortened bitlink.

{
"created_at": "1970–01–01T00:00:00+0000",
"id": "bit.ly/2Km4Nro",
"link": "http://bit.ly/2Km4Nro",
"custom_bitlinks": [],
"long_url": "https://relatable.me",
"archived": false,
"tags": [],
"deeplinks": [],
"references": {
"group": "https://api-ssl.bitly.com/v4/groups/B99h99pjymR"
}
}

Step 3b: Create multiple bitlinks in batch

Slightly more advanced, but infinitely more powerful — taking a file with a link on each line and automatically create bitlinks for each. We’ll use cat to read a text file and the command xargs to run curl (which calls the API) for each line. Finally, we use a feature of jq to format the output into a tab-separated list that we can import into a spreadsheet.

Don’t forget to substitute the access token and group guid with your own.

Example of output (based on input where first column of links are in the links.txt file):


https://relatable.me/ http://bit.ly/2Km4Nro
https://relatable.me/case-studies http://bit.ly/2Ky30zN
https://careers.relatable.me/ http://bit.ly/2Kjz5PJ
https://relatable.me/book http://bit.ly/2KfadZk

To store the output directly as a file, append > filename.txt to the end of the command.

Step 4: Retrieve click statistics

To retrieve the total clicks of a link we use the clicks/summary API method. First, let’s get the clicks for a single link:

Note how the bitlink we want to lookup, http://bit.ly/2Km4Nro is part of the URL we use in the curl command. The forward slash / is replaced by %2F due to url-encoding, and the http:// is omitted.

Step 4b: Retrieve total clicks for multiple bitlinks in batch

For convenience we use a file, shortlinks.txt, where each line is the “hash” of a bitlink. E.g. for the bitlink http://bit.ly/2Km4Nro the hash is 2Km4Nro.

The result returned will be lines corresponding to the input file with the number of total clicks per bitlink.

Troubleshooting

If a command doesn’t work as expected, remove the last part with jq (including the pipe-character). It might reveal an error message that will give you a hint about what was wrong.

Do also note that Bitly has a rate limit of the number of requests you can make to the API. Typically this shouldn’t be a problem. The above examples do not take hitting the rate limit into consideration.

The key take-away is that going from zero to something is often really quick, but still gives you an enormous return on the time spent learning. Often it is more important to know what is possible, rather than knowing exactly how to do something. You can never act on something you don’t know you are not knowing. But you can, on something you know you don’t know.

Knowing how to use the Terminal, a few commands and the basics of API usage you can automate the usage of any API. Here’s a few links related to this article for further reading to step up your digital marketing to the next level:

At Relatable, a marketing tech agency specialized in influencer marketing, automation is essential, no matter if you are in sales, campaign operations or recruitment. Through clever use of technology and automation we create large scale word of mouth marketing with the most creative and influential people around the world. Check out our current job openings to join us, or our case studies for examples of how we can help you.

--

--