Skip to content

Instantly share code, notes, and snippets.

@flowtwo
Last active February 9, 2018 22:57
Show Gist options
  • Save flowtwo/9adb8c3e2716dd80e78a97686783e17a to your computer and use it in GitHub Desktop.
Save flowtwo/9adb8c3e2716dd80e78a97686783e17a to your computer and use it in GitHub Desktop.
ClickDimensions Programmatic Form Capture via PHP cURL
// Define constants
$cd_url = 'https://analytics-eu.clickdimensions.com/forms/h/foobar'; // copy from ClickDimensions
$cd_accountkey = 'foo'; // copy from ClickDimensions
$cd_domain = 'bar'; // copy from ClickDimensions
// Build fields (array) to ClickDimensions, matching keys and values
$fields = array(
'FirstName' => 'John', // retrieve from posted form
'LastName' => 'Doe', // retrieve from posted form
'Email' => 'john@doe.com', // retrieve from posted form
'cd_visitorkey' => 'foobar', // retrieve from the cookie: cuvid
);
// Define header
$headers = array(
'Content-Type: application/x-www-form-urlencoded;',
'Charset: UTF-8;',
'Referer: http://foobar.com/;' // ensure allowed domain is defined at ClickDimensions
);
// Setup cURL
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $cd_url,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => http_build_query( $fields ),
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_VERBOSE => TRUE,
CURLINFO_HEADER_OUT => TRUE
));
$request = curl_getinfo($curl);
// Handle response
$response = curl_exec($curl);
curl_close($curl);
// Print each segment for debugging
echo '<h3>CURL HEADERS</h3>';
echo '<pre>'. print_r($headers, true ). '</pre>';
echo '<h3>CURL POST FIELDS</h3>';
echo '<pre>'. print_r($fields, true ). '</pre>';
echo '<h3>CURL REQUEST</h3>';
echo '<pre>'. print_r($request, true ). '</pre>';
echo '<hr/>';
echo '<h3>CD RESPONSE</h3>';
echo '<pre>'. print_r($response, true ). '</pre>';
@smeranda
Copy link

smeranda commented Dec 6, 2017

@Smus were you able to figure it out? For me, it's posting and a posted form is acknowledged, but no data is stored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment