1

I am trying to send values to the cloud page. my code is this:

%%[
    SET @caseId = [CaseID] 
    SET @lastremdate = [Last Reminder Date]
    SET @Subscriber_ID = [Subscriber_ID]
    SET @link = URLEncode(CloudPagesURL(5455,'caseId',@caseId))
    SET @bitlyUrl = "https://api-ssl.bitly.com/v3/shorten?access_token=XXXXX&longUrl="
    SET @shortenUrl = HTTPGet(Concat(@bitlyUrl,@link,"&format=txt"))
]%%

%%=Format(@lastremdate, "MMM dd, yyyy")=%% is your last reminder date, Please click here %%=v(@shortenUrl)=%%

but it's generating a faulty bitly link. however when I am pasting this code in email studio it's rendering the correct link which is leading me to the cloud page. but I need this in SMS. Please, help!

1
  • 1
    might not mean a thing but i would add a space it between your ]%%%%= to ]%% %%= also you may want to remove your token in your question
    – EazyE
    Commented Dec 3, 2020 at 15:15

1 Answer 1

0

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:

  1. Link address you want to shorten
  2. Access token that you need to generate in your Bit.ly admin panel
  3. Ask Bit.ly to create several Groups if you would want to differentiate your links by brand or region, for example (optional)
  4. Retrieve guid (= Group ID) by doing an API call (optional)
  5. Only once those values are set, you would be able to output the result like - %%=v(@ShortenURL)=%%

Other points to consider:

  1. Check official API documentation, where you can find other things like customizing your links
  2. Set more variables to match the custom field in SMS SendLog for better tracking
  3. Set better error handling, since SSJS is already in place
  4. Add some UTM parameters and concatenate them to the link to improve personalization and tracking
  5. 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .