Merge anonymous lead with known lead with Marketo REST API

428 views Asked by At

I have anonymous leads in Marketo and I need to merge them with known leads through the REST API.

I am trying the code below

public function postData() {
    $url = $this->host . "/rest/v1/leads/" . $this->id ."/merge.json?access_token=" . $this->getToken() . "&leadIds=" . $this::csvString($this->leadIds);

    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 120,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_HTTPHEADER => array(
            "cache-control: no-cache",
            "content-type: application/json",
        ),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);

    curl_close($curl);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        echo $response;
    }
}

It is responding with {"requestId":"1af7#fgfdhgfdgfgdfg","success":false,"errors":[{"code":"1004","message":"Lead '231325' not found"}]} but I can see the lead with the id 231325 in Marketo.

How can I merge the anonymous lead with the known lead with REST?

1

There are 1 answers

0
dferenc On

Unfortunately, you cannot do that, or not in a straightforward way.
The description of the Merge functionality in the Docs does not states it as clearly as the Merge Endpoint reference, but it "merges two or more known lead records into a single lead record". So even if you see the MarketoIds of these visitors in the system, the API won't find them in the Leads table.

However, depending on your exact goal and setup, you might solve your issue by using the Associate Web Activity functionality, as it "associates a known Marketo lead record to a munchkin cookie and its associated web activity history". The Associate Endpoint expects the value of the _mkto_trk cookie of a visitor (sample format: id:287-GTJ-838%26token:_mch-marketo.com-1396310362214-46169).

Having said that, your curl would look something like this:

$url = $host . '/rest/v1/leads/' . $leadId . '/associate.json?cookie=' . $cookie . '&access_token=' . $token;

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
]);

$response = curl_exec($curl);
curl_close($curl);

You can obtain the value of the cookie with $_COOKIE['_mkto_trk'].