Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jupyter-naas
GitHub Repository: jupyter-naas/awesome-notebooks
Path: blob/master/Bitly/Bitly_Get_Clicks_for_a_Bitlink.ipynb
2973 views
Kernel: Python 3

Bitly.png

Bitly - Get Clicks for a Bitlink

Give Feedback | Bug report

Tags: #bitly #api #getclicks #bitlink #python #dev

Last update: 2023-04-12 (Created: 2023-02-24)

Description: This notebook will show how to use the Bitly API to get the click counts for a specified link in an array based on a date.

Input

Import libraries

import requests import naas import json

Setup Variables

  • access_token: Bitly access token. Get your token here.

  • bitlink: Bitlink to get clicks for.

  • unit: Unit of time for the response.

  • units: Number of units of time to query data for.

access_token = naas.secret.get("BITLY_TOKEN") or "<YOUR_TOKEN>" bitlink = "bit.ly/3lU6hRt" unit = "day" units = -1

Model

This function will use the Bitly API to get the click counts for the specified link in an array based on a date.

def get_clicks_for_bitlink(token, bitlink, unit, units): url = f"https://api-ssl.bitly.com/v4/bitlinks/{bitlink}/clicks" headers = {"Authorization": f"Bearer {token}"} params = {"unit": unit, "units": units} response = requests.get(url, headers=headers, params=params) response.raise_for_status() return response.json()

Output

Display result

clicks = get_clicks_for_bitlink(access_token, bitlink, unit, units) print(json.dumps(clicks, indent=4))