This is an expansion on my previous post about the ClickDimensions Cookie Challenge. ClickDimensions is an excellent addon for Dynamics 365 though when it comes to form capture, it lacks what I feel is a fundamental function. Out of the box when a form is submitted the system is only capable of redirecting the end user to either a success or failure page, while this works well for contact us forms it is not as suitable for the likes of a whitepaper download where you want to redirect the user to a specific page based on the context of the form.
To resolve this issue, I created an Azure web app middleware that sits between the website form and ClickDimensions which allows a logic layer and provides far more control on where to redirect the user.
The middleware effectively acts as a buffer between the website and ClickDimensions. By intercepting the response from ClickDimensions, we can control where the user is redirected to.
In this example code, I have used a hidden field on the form to allow the website to pass over the redirect URL to the service. The other option would be to code the redirect logic directly into the Azure App. The method used here give you far more control as it is easier to pass the URL as part of the web form opposed to coding the logic into the application.
Another area to note is that this code has a honeypot field built in to reduce the risk of spam. On each of the form, a hidden field should be created called hp. A real end user will never even know the field exists, but a robot will pick this up as a field to be filled in. This reduces the risk of a bot attack dramatically. I would however still recommend implementing recaptcha.
I have included a link to the sample code on GitHub. The area to note is the mechanism to capture the response from ClickDimensions. For this I have used the stream code below:
private string PostToServer(string FormCaptureURL, string PostData)
{
try
{
string serverResponse = String.Empty;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(FormCaptureURL);
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
byte[] byteArray = Encoding.UTF8.GetBytes(PostData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// set referer
request.Referer = Properties.Settings.Default.Referrer;
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
serverResponse = reader.ReadToEnd();
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
return serverResponse;
}
catch
{
throw;
}
}
Effectively this reads the website response from ClickDimensions as if it was a user on the browser. The redirect contains a single line of text of either success or failure which the stream code reads and redirects accordingly.