APIs & Integrations

paul909
Participant

Submitting Form Data throught API

Hi,

Im running into similar troubles when trying to submit form data through your API.
We’re trying to accumulate data from our backend that then sends information forward to the Hubspot Contact Form. The data arrives with a ‘204’ as expected and is not returning any error.

However, when you do look at the data, its not connecting it with the traffic the user/contact has generated.

This is what our simple example looks like:

public class HubspotTrackingService {

String name;
String lastname;
String phone;
String email;
String company;
//Hubspot Tracking Cookie identifier
String hutk;
String ip;

// log
private static final Logger log = Logger.getLogger(RegistrationApplicationService.class.getName());

public HubspotTrackingService(String name, String lastname, String phone, String email, String company, String hutk, String ip){
    this.name = name;
    this.lastname = lastname;
    this.phone = phone;
    this.email = email;
    this.company = company;
    this.hutk = hutk;
    this.ip = ip;
}


public void send() throws Exception {

    String url = PortalProperties.REGISTRATION_TRACKINGURL_HUBSPOT.getValue();
    try {


        String[] params = new String[]{
                "company", company,
                "firstname", name,
                "lastname", lastname,
                "phone", phone,
                "email", email,
                URLEncoder.encode("hs_context", "UTF-8").toString(),
                        new JSONObject().put("hutk", hutk)
                                        .put("pageUrl", "[REDACTED FOR PRIVACY]")
                                        .put("pageName", "[REDACTED FOR PRIVACY]")
                                        .put("ipAddress", ip).toString()
        };
        String[] headers = new String[]{
                "Content-Type", "application/x-www-form-urlencoded",
        };
        URLPost urlPost = new URLPost();
        URLPost.URLPostResult result = urlPost.postWithResult(url, false, headers, true, params);

        if (result.code == 204 && Comparison.isVoid(result.data)) {
            // it worked.
        } else {
            throw new Exception("Tracking call returned with HTTP status " + result.code);
        }


    } catch(Exception e){
        log.log(Level.SEVERE, "Failed to call registration tracking endpoint " + url, e);
    }
}

public static void main(String[] args){
    HubspotTrackingService trackingService = new HubspotTrackingService("testing", "internal", "123456789",
                            "email@email.de", "company gmbh", "d27029cbe32192da89b7a4539fc0ebda", "[REDACTED FOR PRIVACY]");

    try {
        trackingService.send();
    } catch(Exception e){
        log.log(Level.SEVERE, e.getMessage());
    }
}

}

The post function:

public URLPostResult postWithResult(String url, boolean noSevereOnErrorCode, String[] headers, boolean contentLengthHeader, String... kvpairs) throws Exception {
    // Construct data
    StringBuilder input = new StringBuilder();
    for (int i = 0; i < kvpairs.length; i += 2) {
        String key = kvpairs[i];
        String value = kvpairs[i + 1];
        if (i != 0) {
            input.append("&");
        }
        input.append(URLEncoder.encode(key, "UTF-8")).append("=").append(URLEncoder.encode(value, "UTF-8"));
    }



    // Send data
    URLConnection conn = openConnection(url);

    if (headers != null) {
        for (int i = 0; i < headers.length; i += 2) {
            conn.addRequestProperty(headers[i], headers[i + 1]);
        }
        if(contentLengthHeader){
            conn.addRequestProperty("Content-Length", String.valueOf(input.length()));
        }
    }


    if (input.length() > 0) {
        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
        try {
            wr.write(input.toString());
        } finally {
            wr.close();
        }
    }

    int code = HttpURLConnection.HTTP_OK;
    if (conn instanceof HttpURLConnection) {
        try {
            code = ((HttpURLConnection) conn).getResponseCode();
        } catch (IOException ex) {
            // If the return code is 503 or something, we don't get this, but an IOException instead
            code = HttpURLConnection.HTTP_INTERNAL_ERROR;
        }
        if (code != HttpURLConnection.HTTP_OK) {
            Logger.getLogger(URLPost.class.getName()).log(noSevereOnErrorCode ? Level.WARNING : Level.SEVERE, "failed to access url \"" + url + "\" code=" + code);
        }
    }

    // Get the response
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {
        if ((conn instanceof HttpURLConnection) && (code == HttpURLConnection.HTTP_NOT_FOUND)) {
            HttpURLConnection httpURLConnection = (HttpURLConnection) conn;
            FileUtil.copy(httpURLConnection.getErrorStream(), output);
        } else {
            try {
                FileUtil.copy(conn.getInputStream(), output);
            } catch (Exception ex) {
                HttpURLConnection httpURLConnection = (HttpURLConnection) conn;
                FileUtil.copy(httpURLConnection.getErrorStream(), output);
            }
        }

        output.flush();
        output.close();
    } catch (Exception ex) {
        Logger.getLogger(URLPost.class.getName()).log(Level.SEVERE, "failed to read result stream", ex);
    }

    URLPostResult res = new URLPostResult(url, code, output.toString("UTF-8"));

  
    return res;
}

The request looks like this (encoded):
company=company+gmbh&firstname=testing&lastname=internal&phone=123456789&email=email%40email.de&hs_context=%7B%22hutk%22%3A%22d27029cbe32192da89b7a4539fc0ebda%22%2C%22pageUrl%22%3A%22https%3A%2F%2Fsubdomain.whatever.com%2Fapps%2Fpublic%2Fregistration.html%22%2C%22pageName%22%3A%22Whatever+Test+inc%22%2C%22ipAddress%22%3A%2281.173.204.194%22%7D

Any help would be greatly appreciated.

Thanks

0 Upvotes
8 Replies 8
Derek_Gervais
HubSpot Alumni
HubSpot Alumni

Submitting Form Data throught API

Hi @paul09,

When you say tracking cookie, are you referring to the HubSpot tracking code, or the hubspotutk cookie? Both need to be present for a page view to be tracked. If a visitor views a page that doesn’t have the HubSpot tracking code installed, the views will not be tracked. If you send me a link to a page that you’re visiting I can take a look to see if the tracking code is installed.

Side note: after examining some recent submissions to that form, I noticed that the contacts do not have a usertoken associated with them. Unlike the first test contact you sent, the raw form submission data from the other submissions doesn’t appear to include a hutk value (or any metadata, for that matter). I’m not entirely sure why only that one contact included the relevant metadata, but for those contacts the issue appears to be that the hs_context parameter was missing/malformed.

0 Upvotes
paul909
Participant

Submitting Form Data throught API

Hi @Derek_Gervais,

Thanks for your reply. Both the __hstc and the hubspotutk are present on the webpage.
Ill PM you the link so you can have a look. That a lot of the contacts are malformed is something we are aware of, that comes down to the hs_context being malformed.

Thanks.

0 Upvotes
Derek_Gervais
HubSpot Alumni
HubSpot Alumni

Submitting Form Data throught API

Hi @paul09,

Thank you for the example, that looks as I would expect it to. Is this a single page app? Is the HubSpot tracking code is present and firing on these pages? Everything looks correct internally (other than the lack of page view data)

0 Upvotes
paul909
Participant

Submitting Form Data throught API

Hey @Derek_Gervais,

The webpage is not a single page app. The tracking cookie is there and firing/being updated for each website, im sort of out of ideas what else I can do.

0 Upvotes
Derek_Gervais
HubSpot Alumni
HubSpot Alumni

Submitting Form Data throught API

Hi @paul09,

It looks like the usertoken was successfully included in the form submission, but I’m still not seeing any page views. This often means the hubspotutk cookie was cleared from the visitor’s browser; can you double check that the token you’ve submitted with the form submission is the same one that the contact has in their browser?

0 Upvotes
paul909
Participant

Submitting Form Data throught API

Hi @Derek_Gervais,

That particular contact has been a user contact I have used for debugging purposes only. That hubspotutk is the one I get set as soon as I enter our domain, it gets updated by each page view (to the same value). I can guarantee that it is not being cleared/purged/removed.

Here’s proof that it is being set to the exact same value by each page view(wait for it, its a gif):

To be noted is that this is happening to all of our contacts that we submit using the API call. We have a form submitted here:
https://app.hubspot.com/forms-two/1889922/2751abdf-9e1d-45a9-8057-a1a719aab15c/submissions

Appreciate any help I can get here.

0 Upvotes
Derek_Gervais
HubSpot Alumni
HubSpot Alumni

Submitting Form Data throught API

Hi @paul09,

That encoded request appears to be formatted correctly. Can you send me a link to an example contact in your portal so that I can look at the raw form submission data as it appears in HubSpot?

0 Upvotes
paul909
Participant

Submitting Form Data throught API

Hi Derek,

The contact in question can be found here:

https://app.hubspot.com/contacts/1889922/contact/3901851/?interaction=note

0 Upvotes