Not sure if it is still a relevant question but here is the code snippet you can use to shorten the links with the Bit.ly service. Your code used API v3 and now they fully switched to v4, so the logic is a little bit different.
<script language="javascript" runat="server">
Platform.Load("Core", "1");
try{
</script>
%%[
VAR @Link, @AccessToken, @Payload, @PostRequest, @Response, @ResponseRows, @json, @ShortenURL
/* Change 'xxx' to the link address */
SET @Link = CONCAT('"', "xxx", '"')
/* Change 'xxx' to your personal access token */
SET @AccessToken = 'xxx'
/* Change 'xxx' to the Group ID this link would be associated with and 'yyy' to either default 'bit.ly' domain or your custom one */
SET @Payload = CONCAT('{"group_guid": "xxx", "domain": "yyy", "long_url": ', @Link, '}')
SET @PostRequest = HTTPPost2("https://api-ssl.bitly.com/v4/shorten", "application/json", @Payload, False, @Response, @ResponseRows, "Authorization", CONCAT("Bearer ", @AccessToken))
IF @PostRequest == '200' THEN
SET @json = @Response
ENDIF
]%%
<script language="javascript" runat="server">
/* Receieving json from POST request and parsing it to get the link */
var json = Variable.GetValue("@json");
var ParseJSON = Platform.Function.ParseJSON(json);
var ShortenURL = ParseJSON.link;
Variable.SetValue("@ShortenURL", ShortenURL);
}catch(e){
Write(Stringify(e));
}
</script>
However, to make it work, you would need to get in advance several things:
- Link address you want to shorten
- Access token that you need to generate in your Bit.ly admin panel
- Ask Bit.ly to create several Groups if you would want to differentiate your links by brand or region, for example (optional)
- Retrieve
guid
(= Group ID) by doing an API call (optional)
- Only once those values are set, you would be able to output the result like -
%%=v(@ShortenURL)=%%
Other points to consider:
- Check official API documentation, where you can find other things like customizing your links
- Set more variables to match the custom field in SMS SendLog for better tracking
- Set better error handling, since SSJS is already in place
- Add some UTM parameters and concatenate them to the link to improve personalization and tracking
- Bring the whole batch of code to the SMS by saving the code in the Code Snippet in the Content Builder and then using
%%=ContentBlockByID(...)=%%
in the SMS text