<?php
require_once('api.example.header.php');
require_once(
'iSDK/src/isdk.php');

/*
    Prerequisites:
    
        Inspect the webform to make sure the hidden Ga Fields and Leadsource is appended to the form by iTracker. (Follow Step 1 and Step 2)
        If the fields are added, then adjust the $tracking_data array below to match the $_POST field names and API Custom field names. 
*/

$http_post_action_set_id 217// (optional) Simple action set to send an HTTP post to iTracker360 for Tag based Tracking.

if (isset($_POST['submit'])) {
    if (
$_POST['email'] != "") {
        
$con = new iSDK();
        
$con->cfgCon('dh106'); // connect to your application, make sure you modify the conn.cfg.php
        
        
print "Post Data: ";
        print 
"<pre>";
        
print_r($_POST);
        print 
"</pre>";
        print 
"<br />";
        
        
$contact = array("FirstName" => $_POST['firstname'], "Email" => $_POST['email']);
        
$contact_id $con->addWithDupCheck($contact'Email'); ### Step 3: get the infusionsoft contact id 
        
        
echo "Contact Id: ".$contact_id// we should have the Infusionsoft contact id now
        
print "<br />";
                 
        if (
$contact_id) {
            if (isset(
$_POST['Contact0_GaSource'])) {
                
$tracking_data = array(
                                            
'_GaSource' => $_POST['Contact0_GaSource'],
                                            
'_GaMedium' => $_POST['Contact0_GaMedium'],
                                            
'_GaTerm' => $_POST['Contact0_GaTerm'],
                                            
'_GaContent' => $_POST['Contact0_GaContent'],
                                            
'_GaCampaign' => $_POST['Contact0_GaCampaign'],
                                            
'_GaReferurl' => $_POST['Contact0_GaReferurl'],
                                            
'_IPAddress' => $_SERVER['REMOTE_ADDR'], // optional GeoIP Tracking Field                                        
                                            
'Leadsource' => $_POST['LeadSource'// dynamicaly created via iTracker  (case-sensitive)                                     
                                        
);  ### Step 4: Make a tracking array for data to be stored inside Infusionsoft.

                
print "Tracking Data: ";
                print 
"<pre>"
                
print_r($tracking_data);
                print 
"</pre>";
                print 
"<br />";
                                                   
                
### Step 5 & 6: Update the contact record with tracking data, run an action set to "Send an HTTP Post" to our server for tag based tracking.                                                           
                
$con->updateCon((int)$contact_id$tracking_data); // Store the tracking data inside Infusionsoft via the Custom Fields
                
if ($http_post_action_set_id) {
                    
$con->runAs($contact_id, (int)$http_post_action_set_id); // Action set to send HTTP post to iTracker360.com if you want to use Tag Based Tracking and GeoIP Tracking
                
}
            }
        } 
    }
}

?>
<html>
<head>
<title>Simple API Example</title>

<!-- ### Step 1: include itracker snippet for your account -->
<script type="text/javascript" src="https://d2ieqaiwehnqqp.cloudfront.net/t2dc87e0440388a01359775bd16f6ac1c.js"></script>
</head>
<body>


<!-- ### Step 2: Make the form recognizable for iTracker by adding infusion_form1 for the id field -->
<form id='infusion_form1' action='index.php' method='post'>
    <input type="hidden" name="app_name" value="dh106" />
    First Name: <input type='text' name='firstname' value='' /><br />
    Email Address: <input type='text' name='email' value='' />
    <input type='submit' name='submit' value='submit' /> <br />
</form>


<a href="source.php">Full Source Code</a> | <a target="_blank" href="https://www.youtube.com/watch?v=Doxu-Y1D4sI">Walk Through Video</a>

</body>
</html>