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>';
@pamelalies
Copy link

pamelalies commented Jun 17, 2017

I implemented this, and did not use these two lines at all, and it works just fine without them!

$cd_accountkey = 'foo'; // copy from ClickDimensions
$cd_domain = 'bar'; // copy from ClickDimensions

@flowtwo
Copy link
Author

flowtwo commented Jul 12, 2017

Hi @trsteel88 and @pamelalies

Sorry for the late response. No, $cd_accountkey and $cd_domain are not used in this section, but they are required while using their javascript to set the $cd_visitorkey .. and must have left them here by mistake.

@Smus
Copy link

Smus commented Aug 11, 2017

Hi @flowtwo and @pamelalies
I managed to create new Contacts, but it only use the email address for the new Contact, it dosnt apply the FirstName and LastName aswell... any ideas?

$fields = array(
		'Email' => $email,
		'LastName' => $lastname,
		'FirstName' => $firstname,
		'JobTitel' => "tester",
		'score' => $score,
		'cd_visitorkey' => '123',
		'cd_accountkey'=> $cd_accountkey,
		'cd_domain'=> $cd_domain
)

@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