Previously, we've looked at how to use the Eloqua API to pull Contacts, Data Cards, and Activity data from an Eloqua instance.

 

But what about data related to Visitors?

 

In this post, we will see an example of how to query for Visitor Profile Data stored in an Eloqua instance.

 

Once again, we will assume that the code found here is being used to establish a connection to the Eloqua API, and we will be using the EloquaService API (see Eloqua API - Which one do I use?).

 

The code is the same as that used for Eloqua API How To: Query a Contact by Email Address, with the exception of entityType.Name being "Visitor" instead of "Contact", and the use of a different search term.

 

In this snippet, we will use the Visitor field called "V_FirstVisitDateAndTime".  We will use the search term V_FirstVisitDateAndTime>2012-03-01 to bring back all Visitor data since March 1st, 2012.  You may choose to query using other fields, in this case, please see Eloqua API How To: Describe an Entity and Eloqua API How To: Query a Contact by Multiple Fields for more information on how to do this. 

 

(Note: Eloqua Visitor profile data is read-only)

 

Now, to the code...

 

    class QueryVisitorData

    {

        static void Main()

        {

           const string searchQuery = "V_FirstVisitDateAndTime>2012-03-01";

 

            try

            {

               //Create the service instance using your credentials

               EloquaInstance service = new EloquaInstance("instance", "userid", "password");

 

                // Instantiate a Visitor Entity Type

                EntityType visitorEntityType = new EntityType

                               {

                                  ID = 0,

                                  Name = "Visitor",

                                  Type = "Base"

                               };

 

                // Create a new list containing the fields you want populated

                // We will leave this empty for now, which will cause the code to return all fields

                List<string> fieldList = new List<string>();

 

                // Define a container for the Query results

                DynamicEntityQueryResults queryResult;

 

                // Set the page number and size for the results

                const int currentPage = 1;

                const int pageSize = 20;

 

                // If the field list is empty - the request will return all Entity Fields

                if (fieldList.Count == 0)

                {

                    // Execute the request and return all of the Entity's fields

                    queryResult = service.ServiceProxy.Query(visitorEntityType, searchQuery, null, currentPage, pageSize);

                }

                else

                {

                    // Execute the request and return only the selected fields

                    queryResult = service.ServiceProxy.Query(visitorEntityType, searchQuery, fieldList.ToArray(), currentPage, pageSize);

                }

 

                if (queryResult.Entities.Length > 0)

                {

                    //Extract the total number of pages and records

                    Console.WriteLine(String.Format("Total number of pages: {0}", queryResult.TotalPages));

                    Console.WriteLine(String.Format("Total number of records: {0}", queryResult.TotalRecords));

 

                    // Extract each Dynamic Entity in the result

                    foreach (DynamicEntity dynamicEntity in queryResult.Entities)

                    {

                        // Extract the field name and value of each field in the collection

                        foreach (KeyValuePair<string, string> field in dynamicEntity.FieldValueCollection)

                        {

                            Console.WriteLine(String.Format("Field Name: {0}: {1}", field.Key, field.Value));

                        }

                    }

                }

            }

 

            // Customize your own error handling code

            catch (System.ServiceModel.FaultException ex)

            {

                // Catch Service Model Fault Exceptions

                Console.WriteLine(String.Format("Reason: {0}", ex.Reason));

                Console.WriteLine(String.Format("Fault Type: {0}", ex.GetType()));

                  Console.WriteLine(String.Format("Fault Code: {0}", ex.Code.Name));

            }

            catch (Exception ex)

            {

                // Catch System Exceptions

                Console.WriteLine(String.Format("Exception Message: {0}", ex.Message));

            }

 

            // Wait for user input before stepping out.

            Console.WriteLine("Press any key to continue.");

            Console.ReadKey();

        }

    }