0

Anyone using bit.ly is probably aware of the deprecation of their v3 API.

I thought I had bit.ly v4 done and dusted, but when sending it to a test DE I noticed no bit.ly URL was rendered.

Expected output: 'Hi your account is overdue, please make a payment here: nrg.io/Th2jk

this is also what we see in the subscriber preview in journey studio when testing the below code

This is the SMS received: Hi your account is overdue, please make a payment here:

I am wondering if it's better to do Bit.ly URL generation in automation studio as a step when the file is dropped, but that would mean difficulty getting EncryptSymetric() to work. What sort of error handling should I do to determine what is going on? Is there a way to synchronise the SSJS request or set a timeout?

Also worth noting, we have a Bit.ly Dev account and should be ok at 1000 requests/min. Well above our test send.

Platform.Load("Core", "1");
try{
</script>%%[
    VAR @Link, @FullURL, @AccessToken, @Payload, @PostRequest, @Response, @ResponseRows, @json, @ShortenURL
  /* Import and set variables */
  SET @FirstName = AttributeValue("FirstName")
  IF @Name =="" THEN  SET @Name = "there"
  ELSE SET @Name = ProperCase(trim(@FirstName))
  ENDIF

  SET @Account = AttributeValue("AccountNumber")
  SET @ID = Base64Encode(AttributeValue("ID"))
  SET @Password = "Password"
  SET @Salt = "Salt"
  SET @IV = "IV"
  SET @encAES = EncryptSymmetric(@ID, "AES", @Password, @null, @Salt, @null, @IV, @null)
  SET @Link=URLEncode(Concat("EXAMPLE.COM?Nid=",@encAES))
      /* Prepare the full link */
      IF NOT EMPTY(@Link) THEN
          SET @FullURL = URLEncode(CONCAT('"', @Link, '"'))
      ENDIF

      /* bitly access token */
      SET @AccessToken = 'Access Token'

      /* 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": "GROUP_GUID", "domain": "example.io", "long_url": ', @FullURL, '}')
      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>Your acct: %%=v(@Account)=%% See more about your options here: %%=v(@ShortenURL)=%%

1 Answer 1

0

I am assuming there is no errors in your encoding URL part.

So I took a look on the rest part and found that

  • You have the issue in your @Payload
  • Need to string the response before you can parse the JSON since when you get the response it is in another format.

Here is the version I tried to modify from your code

%%[
  VAR @Response, @ResponseRows
  SET @FullURL = "https://example.com" /* TEST URL */
  SET @AccessToken = 'YOUR BITLY TOKEN'
  SET @endpoint = "https://api-ssl.bitly.com/v4/shorten"
  set @contentType = "application/json"
  SET @header_name = 'Authorization'
  SET @header_value = Concat('Bearer ', @accesstoken)

  /* 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('{"domain": "bit.ly", "long_url": "', @FullURL, '"}')
  SET @PostRequest = HTTPPost2(@endpoint, @contentType, @Payload, False, @Response, @ResponseRows, @header_name, @header_value)


  SET @json = @Response
]%%

<script language="javascript" runat="server">
Platform.Load("core", "1.1.1");
    /* Receieving json from POST request and parsing it to get the link */
    var json       = Variable.GetValue("@json");
    var ParseJSON  = Platform.Function.ParseJSON(String(json));
    //Write(Stringify(ParseJSON)); PRINT RESULT

    var ShortenURL = ParseJSON.link;
    Variable.SetValue("@ShortenURL", ShortenURL);

</script>Your acct: %%=v(@Account)=%% See more about your options here: %%=v(@ShortenURL)=%%
6
  • Hey, Thanks for the response and your time looking at my problem. So I added var ParseJSON = Platform.Function.ParseJSON(String(json)); as described above, and retested the SMS. It works for the first Row in the DE (first SMS), and I get a shortened URL; subsequent rows in the DE that are sent out do not have a URL at the end of the text. Again no errors when looking at the subscriber preview.
    – Tom
    Commented Jan 6, 2023 at 8:17
  • So uncommenting Write(Stringify(ParseJSON)) and test sending on the DE against new URLs I want to be generated, I noticed on calls to the bit.ly API I get a "NULL" as a response. When testing against a previously tested set. I seem to get the links returned.
    – Tom
    Commented Jan 6, 2023 at 9:36
  • @Tom So the code works? I think you may want to check in your Bitly account to see whether you reach the limitation in the account
    – Duc Le
    Commented Jan 6, 2023 at 9:45
  • I've reached out to Bit.ly and awaiting a response, we have a dev account and are currently at 500 generated links out of 65000 for the month. No way we are hitting our limit. My test DE is currently 32 rows. That being said I'm not ruling out some obscure limitations with the new API like concurrent connections from the same IP. Im getting the Null because the Post request when checked is '201' IF @PostRequest == '200' THEN SET @json = @Response ENDIF something here, maybe?
    – Tom
    Commented Jan 6, 2023 at 10:04
  • IF @PostRequest == '200'OR @PostRequest == '201' THEN SET @json = @Response ENDIF This ended up fixing it! I wasn't always getting a 200 response from bit.ly but a 201 for newly created objects. Thanks for your help @Duc Le
    – Tom
    Commented Jan 6, 2023 at 10:19

You must log in to answer this question.

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