Search code examples
restgoogle-sheetsquery-stringbitly

Trying to Add tags to Bitly API Query String


I am creating a spreadsheet to help me quickly shorten UTM parameters for ad campaigns. I want to be able to dynamically add tags to the query string so that they are easier to organize once they are created in Bitly. Here is what I have tried in Google Sheets.

=importData(concatenate("https://api-ssl.bitly.com/v3/shorten?tags[]=test&tags[]=new&longUrl=",ENCODEURL(J10),"&access_token=",$C$5,"&format=txt"))

It kicks back a URL and works perfectly, except it does not add the tags. Just not sure what I am missing.

Link to copy, just add bitly API key: https://docs.google.com/spreadsheets/d/1OB4CaA-P-dXpRsDXjdTzLkNKHWGPjkzWOxo3n-Iv6ao/edit?usp=sharing

To further explain my end result... you will notice my "tags" parameter in the URL. I want that to be pushed into the Bitly API and create/add those tags to the newly created URL. This will give me the ability to better filter them for use.


Solution

  • Even though the docs says you can, it seems like it is not possible to create the link with tags. Even the Bitly UI does not support it.

    It is possible to do an update and add the tags. I created the bitlink and then right after I did another request to updated it with the tags. Update API: PATCH:https://api-ssl.bitly.com/v4/bitlinks/{bitlink.id}

    This is what I did:

     fetch("https://api-ssl.bitly.com/v4/shorten", {
                method: "POST",
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': 'Bearer ***'
                },
                body: JSON.stringify(data)
            }).then(response => response.json())
                .then(data => {
                    let updateData = {
                        "tags": [
                            "tag1",
                            "tag2"
                        ]
                    };
    
                    fetch("https://api-ssl.bitly.com/v4/bitlinks/" + encodeURIComponent(data.id), {
                        method: "PATCH",
                        headers: {
                            'Content-Type': 'application/json',
                            'Authorization': '***'
                        },
                        body: JSON.stringify(updateData)
                    })
    
                });
        }