0

We want to use all links to be directed to cloudpage(cloudpage is doing some action), then immediately to the same link(not a specific link) which has been used across email. How would it possible? Any insight will be helpful.

<a href="%%=RedirectTo(CloudPagesURL(pageid, 'redirectTo', 'link'))=%%>link</a>

This is not working when put inside every email link.

Cloud page code below.

%%[
SET @emailAddress = AttributeValue("Email")
SET @FirstName = AttributeValue("FirstName")
SET @redirectto = RequestParameter(redirectto)

IF NOT EMPTY(@emailAddress) THEN
]%%
    
<script runat="server">
Platform.Load("Core", "1");
try {
  
    var email = Variable.GetValue("@emailAddress");
    var FirstName = Variable.GetValue("@FirstName");
        //authenticate to get access token
    var authEndpoint = 'subdomain.auth.marketingcloudapis.com/';  //add your tenant specific endpoint
    var payload = {
        client_id: "clientid",            //add your client id
        client_secret:"client_secret",     //add your client secret
        grant_type: "client_credentials"
    };
    var url = authEndpoint + '/v2/token';
    var contentType = 'application/json';

    var accessTokenRequest = HTTP.Post(url, contentType, Stringify(payload));
    if (accessTokenRequest.StatusCode == 200) {
        var tokenResponse = Platform.Function.ParseJSON(accessTokenRequest.Response[0]);
        var accessToken = tokenResponse.access_token;
        var rest_instance_url = tokenResponse.rest_instance_url;
    };
      //make api call to release subscriber 
    if (email != null && accessToken != null) {
        var headerNames = ["Authorization"];
        var headerValues = ["Bearer " + accessToken];
        var jsonBody = {
            "ContactKey": email,                      //pass the Contact Key
            "EventDefinitionKey": "APIEvent-XXXXXXXXXXXXXXXXXX",   //add the  API Key
            "Data": {
                "Email": email,
                "FirstName": FirstName
                
            }
        };
        
        var requestUrl = rest_instance_url + "/interaction/v1/events";
        
        var fireEntryEvent = HTTP.Post(requestUrl, contentType, Stringify(jsonBody), headerNames, headerValues);
                
        };
}
catch (error) {
    Write("<br> something went wrong.<br>" + Stringify(error));
}
</script>
%%[
Redirect(@redirectto)
]%%
%%[ELSE]%%

%%[ENDIF]%%
4
  • Is this the literal implementation? Or are you putting the actual link in the function call? E.g. RedirectTo(CloudPagesURL(123, 'redirectTo', 'example.com')) ? Nov 18 at 12:31
  • Yes Lukas I am putting actually link inside, But it is doing the action in cloud page, but after that It is not redirecting to the email link.
    – Pravat
    Nov 18 at 12:33
  • Please share your CloudPage code in your updated question Nov 18 at 12:34
  • Cloud page code updated - Lukas . Thanks
    – Pravat
    Nov 18 at 12:51
0

There were few issues in your code. Also, try redirecting in SSJS instead of in AMPScript:

%%[
set @subscriberKey = RequestParameter(_subscriberKey)
set @emailAddress = RequestParameter(emailaddr)
SET @FirstName = AttributeValue("FirstName")
SET @redirectto = RequestParameter("redirectto")
IF NOT EMPTY(@emailAddress) THEN
]%%
    
<script runat="server">
Platform.Load("Core", "1");
try {
  
    var email = Variable.GetValue("@emailAddress");
    var FirstName = Variable.GetValue("@FirstName");
    var redirectto = Variable.GetValue("@redirectto");

        //authenticate to get access token
    var authEndpoint = 'subdomain.auth.marketingcloudapis.com/';  //add your tenant specific endpoint
    var payload = {
        client_id: "clientid",            //add your client id
        client_secret:"client_secret",     //add your client secret
        grant_type: "client_credentials"
    };
    var url = authEndpoint + '/v2/token';
    var contentType = 'application/json';

    var accessTokenRequest = HTTP.Post(url, contentType, Stringify(payload));
    if (accessTokenRequest.StatusCode == 200) {
        var tokenResponse = Platform.Function.ParseJSON(accessTokenRequest.Response[0]);
        var accessToken = tokenResponse.access_token;
        var rest_instance_url = tokenResponse.rest_instance_url;
    };
      //make api call to release subscriber 
    if (email != null && accessToken != null) {
        var headerNames = ["Authorization"];
        var headerValues = ["Bearer " + accessToken];
        var jsonBody = {
            "ContactKey": email,                      //pass the Contact Key
            "EventDefinitionKey": "APIEvent-XXXXXXXXXXXXXXXXXX",   //add the  API Key
            "Data": {
                "Email": email,
                "FirstName": FirstName
                
            }
        };
        
        var requestUrl = rest_instance_url + "/interaction/v1/events";
        
        var fireEntryEvent = HTTP.Post(requestUrl, contentType, Stringify(jsonBody), headerNames, headerValues);
        Platform.Response.Redirect(redirectto)       
        };
}
catch (error) {
    Write("<br> something went wrong.<br>" + Stringify(error));
}
</script>
%%[ELSE]%%
%%[ENDIF]%%
5
  • Also, @Pravat, I would avoid using email address as contact key, but that's a whole different topic Nov 18 at 13:29
  • Thanks Lukas, But it is still not working and showing 500 -Internal server error.
    – Pravat
    Nov 18 at 13:54
  • OK, I need to see if I can do some tests later Nov 18 at 13:55
  • Hi Lukas, I am able to solve it. Now it is working. But the problem is with the subscriberkey. If I assign the ContactKey as subscriberkey, I am getting error which I should not. If you have any suggestion let me know.
    – Pravat
    Nov 18 at 14:55
  • One more problem is when the link is clicked once it is fine, but if click more than once it is showing error as the subscriber is already in that DE. So how would we overcome this issue to redirect to the destination url rather than showing error. Also there are multiple links in email, only one link will work where as other links are having error for the same subscriber. Any suggestion would be helpful.
    – Pravat
    Nov 19 at 6:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.