Custom form tracking



We currently have a custom form that communicate with our backend server for users to download our software, since it is custom coded we are not accurately tracking where our users are coming from and the conversion rate of each traffic channel.

Looking at the form on our site, I see that it's calling a .php document with this code snippet:

// May 2016 - Post Free Downloads leads to HubSpot too
  $hs_form_id = "317069ee-049f-4db1-9934-20311bef48a1";
  // $hs_pageUrl = "";
  $hs_pageUrl = "";
  $hs_pageName = "RTI Free Downloads Application";
  $result = post_to_hubspot($hs_form_id, $clean_post, $hs_pageUrl,

where post_to_hubspot is an additional .php form for additional code:


    indent preformatted text by 4 spaces
/**`Preformatted text`
 * Submit form data to HubSpot
 * Process a new form submission in HubSpot in order to create a new Contact.

function post_to_hubspot2($formId, $params, $hs_pageUrl = null, $hs_pageName = null) {
    //grab cookie from browser
    if (isset($_COOKIE['hubspotutk'])) {
        $hubspotutk = $_COOKIE['hubspotutk'];
    } else {
        $hubspotutk = "";
    $ip_addr = $_SERVER['REMOTE_ADDR']; // log IP address too.

    $hs_context = array(
        'hutk' => $hubspotutk,
        'ipAddress' => $ip_addr,
		'pageUrl': "", 
		'pageName': "Download RTI Connext DDS Professional", 

    // Two optional parameters
    if (!empty($hs_pageUrl)) {
        $hs_context['pageUrl'] = $hs_pageUrl;

    if (!empty($hs_pageName)) {
        $hs_context['pageName'] = $hs_pageName;

    $hs_context_json = json_encode($hs_context);

    //Need to populate these varilables with values from the form.
    $str_post = http_build_query($params);
    $str_post .= "&hs_context=" . urlencode($hs_context_json); //Leave this one be

    //replace the values in this URL with your portal ID and your form GUID
    $endpoint = '' . $formId;

    // error_log("Posting to form $formId with added data " . print_r($hs_context, true), 0);
    error_log("curl " . $endpoint . $formId . "?" . $str_post, 0);

    $ch = @curl_init();
    @curl_setopt($ch, CURLOPT_POST, true);
    @curl_setopt($ch, CURLOPT_POSTFIELDS, $str_post);
    @curl_setopt($ch, CURLOPT_URL, $endpoint);
    @curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/x-www-form-urlencoded'
    @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response    = @curl_exec($ch); //Log the response from HubSpot as needed.
    $status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE); //Log the response status code


// vim: expandtab sw=4 tabstop=4 softtabstop=4

As of right now, we are getting the # of downloads and contact information. However we are not getting conversion rate for all pageviews, or conversion rate for each of the traffic channel.

Any insights on what we are currently missing on the code snippets posted above is greatly appreciated.

Thank you!


Hey @dtran, are you referencing the stats that you see on a Form such as these?:

If so, as per this topic: Tracking Pageviews of Form Loaded through an API call this metric is only tracked when a HubSpot form embed code is fired, so submissions made through the API does not affect it.


hi @Connor_Barley, yes!

Currently we have create a Hubspot form and it seems that we are pushing the correct Contact information to our system but we are lacking information such as pageview, conversion rate, # of new customers, submission per traffic channel etc...

I also saw this knowledge article yesterday on Hubspot:
and followed the instructions, it now seems like I'm getting submission data on this form.

However, it's being registered as an Unidentified form on Hubspot. Based on the article it seems that we can clone this to Hubspot. Since I have created a similar form on Hubspot since 2016, is there any way for me to link this unidentified form to an existing Hubspot form?

Another problem that I noticed is that it's grouping 2 form data into one since we have 2 non-hubspot forms.
As you can see here:

Are there any ways I can edit that?


Hi @drtan since that form is a non-HubSpot form, you won't get any tracking on conversion rate etc. In my last response:

this metric is only tracked when a HubSpot form embed code is fired, so submissions made through the API does not affect it.

You can always rename that form if you'd like, though! Just hover over the form, then click Actions > Rename.

To make the form data not be grouped together, I'd recommend replacing them with native HubSpot forms and embedding them on your external page. That way, you'd get conversion rate as well.


hi @Connor_Barley,

Unfortunately since this form connects with our backend server, we cannot use a native Hubspot form. I have contacted one of our Hubspot account manager and the recommended approach was to use the form tracking API as mentioned.

I'm a little confused why it's unable to track conversion rate though. If we have pageview and submission data, wouldn't it be simple to get conversion?


Hi @dtran, the reason is because, as Isaac mentions in that thread I linked above, the form views metric is only tracked when a HubSpot form embed code is fired. On the backend we then calculate the submission rate as: (submissions รท views) * 100.

Since views would be empty, we cannot calculate this accurately and cannot rely on browser sessions due to a number of things like bot activity, how we calculate sessions instead of page views etc. The pixel that fires when a form is loaded is quantifiable on the backend and we can use that to calculate the conversion rate, but we can't do the same with the tracking code


hi @Connor_Barley, thanks for answering my question. It seems that I was looking at Isaac's reply and inferring form views as page views.

Is it not correct to assume that it is the same given that the form is on the page itself?


Hi @dtran, correct - form views vs page views are not calculated in the same manner. Form views are calculated when a pixel in the embedded form is loaded. Page views are much more complex in nature and are thus not used or available when looking at Form Submission rate.