Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Businesses that have their own software solution or backend process that want to pull website visitor tracking results into their system own software solution or backend processes can use Lead Liaison's visitor tracking API web services. In the example below,   we use the Get Visits (retrieves visitors) and Get Hits (retrieves web pages) API web services to retrieve visitor data. The same code below is written in Java. Learn more about the API here

https://app.leadliaison.com/api-documentation/index.php#api-Visits-GetVisits (use visit ID)

https://app.leadliaison.com/api-documentation/index.php#api-Hits-GetHits

Code Block
languagejava
themeMidnight
titleQuerying the API
linenumberstrue
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
//get this org.json library from http://www.java2s.com/Code/JarDownload/java/java-json.jar.zip
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class LeadLiaisonAPIClient {

    public static void main(String[] args) throws IOException, JSONException {
        // Uses the Get Visits API web service
		StringBuffer content = getRestResponse(
                "https://api.leadliaison.com/v1.0/visits.json?api_key=PUT_KEY_HERE&from_date=2018-03-01&include_prospect_info=1&show_isp=0");

        JSONObject jsonObject = new JSONObject(content.toString());
        JSONArray records = (JSONArray) jsonObject.get("records");

        //ArrayList<Integer> vistorList = new ArrayList<Integer>();

        for (int i = 0; i < records.length(); i++) {
            //vistorList.add(records.getJSONObject(i).getInt("visitor_id"));
			// Uses the Get Hits API web service
            StringBuffer hitRep = getRestResponse(
                    "https://api.leadliaison.com/v1.0/hits.json?api_key=PUT_KEY_HERE&visit_id="
                            + records.getJSONObject(i).getInt("visitor_id"));
            	// Remove comments in the line below to print the web pages viewed by the visitor.
				// System.out.println(hitRep);
        }

    }

    private static StringBuffer getRestResponse(String urlString)
            throws MalformedURLException, IOException, ProtocolException {
        URL url = new URL(urlString);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("GET");

        int responseCode = con.getResponseCode();
        if (responseCode != 200)
            throw new RuntimeException("ResponseCode: " + responseCode);
        else {
            
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer content = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                content.append(inputLine);
            }
            in.close();
            con.disconnect();
            return content;
        }
    }
}