Programmatic Form Capture

NOTE: Programmatic form capture is an open-source community-supported project. This information is provided without warranty and is subject to change. ClickDimensions does not provide technical support for custom code that you develop. If you have questions about these code samples or how to get your programmatic form captures to work, post them on our Q&A forum.

ClickDimensions Form Capture is a service that allows form post data to be synchronized into Microsoft CRM. Once a Form Capture is configured, under the ClickDimensions Settings area, an Action URL is provided by ClickDimensions which represents the location to which the specific form will be submitted. There are 2 options to post data to the Form Capture location:

  • Replace the action attribute in existing HTML Form. This is the method previously outlined in this guide. For example, consider the following form:
    <form action= “http://analytics.clickdimensions.com/forms/h/aQmmV6psLk6yLb12C6j” method=”post” name=”frmWebCapture” id=”frmWebCapture”>
    <input id=”txtEmail” size=”25″ name=”txtEmail” />
    <input type=”submit” value=”go” id=”emaillistbtn” name=”Submit”>
    </form>

    The “action” attribute represents the location of the form capture. The txtEmail is the Email field which will be posted once the user clicks the submit button.

Important: the ClickDimensions tracking script must be present inside the page that contains the form. It must be after the closing </form> tag, and before the closing </body> tag.

  • Programmatically post the data using server side code. In order to implement this scenario, you must provide special parameters to the posted data which the Form Capture expects. These are:

    a. Visitor Key (parameter name: cd_visitorkey): Again, the form page must include the ClickDimensions tracking script for this to work. The script creates a cookie which contains a unique Visitor Key that is associated with the visitor who submits the form. There are 2 version of the key format:v[number]_[UUID]
    or
    UUIDFor example:v1294610734264_71186AF1FE674FE78A5CEEF96E0FB1A2
    or
    71186AF1FE674FE78A5CEEF96E0FB1A2The cookie name which holds this value is cuvid. In order to retrieve the cookie value you can use JavaScript as described here: http://www.w3schools.com/JS/js_cookies.asp 

    b. Referrer Header: the referrer value determines the domain from which the form was posted. Because ClickDimensions validates domain origin for all analytics requests, it is important to include the web site domain.Here is an example of posted data which was captured by Fiddler:POST http://analytics.clickdimensions.com/forms/h/aZ7R5w1EbG0CIwwRohiwAW HTTP/1.1Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, */*Referer: http://mysite.com/ Accept-Language: en-US,he-IL; q=0.5User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; MDDC; .NET4.0C; .NET4.0E; MS-RTC LM 8; InfoPath.3) Content-Type: application/x-www-form-urlencoded Accept-Encoding: gzip, deflateHost: analytics.clickdimensions.com Content-Length: 105 Connection: Keep-AlivePragma: no-cache Cookie: cuvid=v2294610734264_71186AF55E674FE78A5CEEF96E0FB1A2; cusid=1295380830094; cuvon=1295380830115; txtEmail=john@domain.com&Submit=go&cd_visitorkey= v2294610734264_71186AF55E674FE78A5CEEF96E0FB1A2

    The above data has been POSTed to the ClickDimensions Form Capture URL.

    Please note the cd_visitorkey value which is concatenated to the posted fields’ data (in this case it is a simple email address). The referrer value points to http://mysite.com because the form was posted from the mysite.com web site.

    It may also be helpful to refer to the MSDN article titled “How to: Send Data Using the WebRequest Class” at http://msdn.microsoft.com/en-us/library/debx8sh9.aspx. The article contains an example of how to post form data programmatically.

Download the Code Sample:

A full example in C# for sending posted data to form capture can be found here:

zip http://clickdimensions.com/download/ClickDimensions.FormCapture.zip

 

Update: The below Java sample was shared by a ClickDimensions customer:

We successfully post to form captures with server side java code. Below is the central code. We’re not using the static success and error redirect urls defined in the Clickdimensions form captures, so I’ve just set them to ‘http://success‘ and ‘http://error‘. The method is checking response to decide if the post has succeeded or not.

private boolean createCRMContent(HttpServletRequest request) throws Exception {

 final String referer = request.getHeader("referer");
 final String visitorKey = getCookieValue(request.getCookies(), "cuvid", "-1");
 String postData = "cd_visitorkey=" + visitorKey;
 Enumeration<String> paramNames = request.getParameterNames();

 while (paramNames.hasMoreElements()) {
 String paramName = (String)paramNames.nextElement();
 String paramValue = request.getParameter(paramName);
 postData += "&" + paramName + "=" + URLEncoder.encode(paramValue, "UTF-8");
 }

 HttpURLConnection connection = null;

 try {

 // Create connection
 URL url = new URL("http://analytics.clickdimensions.com/forms/h/...");
 connection = (HttpURLConnection) url.openConnection();
 connection.setDoOutput(true);
 connection.setDoInput(true);
 connection.setInstanceFollowRedirects(false);
 connection.setRequestMethod("POST");
 connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
 connection.setRequestProperty("charset", "utf-8");
 connection.setRequestProperty("Referer", referer);
 connection.setRequestProperty("Content-Length", "" + Integer.toString(postData.getBytes().length));
 connection.setUseCaches(false);

 // Send request
 DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
 wr.writeBytes(postData);
 wr.flush();
 wr.close();

 // Get response
 InputStream is = connection.getInputStream();
 BufferedReader rd = new BufferedReader(new InputStreamReader(is));
 String line;
 StringBuffer response = new StringBuffer(); 
 while((line = rd.readLine()) != null) {
 response.append(line);
 response.append('\r');
 }
 rd.close();

 return response.toString().toLowerCase().contains("http://success&quot ;

 } catch (Exception e) {

 throw e;

 } finally {

 if(connection != null) {
 connection.disconnect(); 
 }

 }

}

public static String getCookieValue(Cookie[] cookies, String cookieName, String defaultValue) {
 for(int i=0; i < cookies.length; i++) {
 Cookie cookie = cookies;
 if (cookieName.equals(cookie.getName()))
 return(cookie.getValue());
 }
 return(defaultValue);
}
About Matt

Director, Technical Solutions at ClickDimensions
Microsoft Dynamics CRM MVP
Author, The Microsoft Dynamics CRM 2011 Administration Bible