title: PHP: create a cookie from Google Adword campaign keyword URL
body: <p>I am using PHP on a Wordpress site to do this. If you think JavaScript or jQuery would be better, let me know.</p>
  
  <p>One of our partners is running Google Google Adword campaigns for us. I don't have access to the exact campaign URLs so I can't create cookies based on knowing the campaign URLs. The business owner wants to track which campaign users came from when they submit a form on our site. My plan:</p>
  
  <ol>
  <li>I want to set/name a cookie based on Google Adword campaign keyword URL.</li>
  <li>If a cookie is set from a previous campaign and the user clicks on a new campaign I want to remove the previous campaign cookie (if it exists) and create a cookie from the new campaign.</li>
  <li>If this cookie is set - and they are on the form page - I want to load a hidden input with the cookie name in the value attribute - and when the form is submitted, that hidden field would tell us which campaign they came from.</li>
  </ol>
  
  <p>Here is an example of an Adword campaign URL our partner is using: </p>
  
  <p>?UTM_keyword=field%20service%20software_phrase&amp;UTM_source=Paid%20Search&amp;gclid=EAIaI</p>
  
  <p>What I am trying to do is grab the portion of the above URL between "?UTM_keyword=" and "&amp;UTM_source=" - in this case, it would be "field%20service%20software_phrase". </p>
  
  <p>Here is the code I've developed so far:</p>
  
  <p>header.php - after <code>&lt;!DOCTYPE html&gt;</code> on every page</p>
  
  <pre><code>&lt;?php
  // load content-utm.php if the URI contains UTM_keyword
      if (strpos($_SERVER['REQUEST_URI'], "UTM_keyword")){
  // load content-utm.php
          get_template_part( 'content', 'utm' );
      }
  ?&gt;
  </code></pre>
  
  <p>content-utm.php</p>
  
  <pre><code>&lt;?php
      $uriSegments = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
  // strip out UTM_keyword=
      $uriSegments2 = substr($uriSegments, 12);
  // strip out everything after &amp;UTM_source= including &amp;UTM_source=
      $uriSegments3 = substr($uriSegments2, 0, strpos($uriSegments2, "&amp;UTM_source="));
      $_SESSION['uriName'] = $uriSegments3;
  // create a cookie
      if (strpos($_SERVER['REQUEST_URI'], $uriSegments3)){
          $cookie_name = $uriSegments3;
          $cookie_value = "UTM";
  // make it a session cookie that expires after the browser is closed
          setcookie($cookie_name, $cookie_value, 0, "/");
      }
  // I know how to remove a cookie but I'm not sure how I would remove THIS type of cookie
  ?&gt;
  </code></pre>
  
  <p>form-page.php</p>
  
  <pre><code>&lt;?php
  // if the cookie is set
      if (isset($_COOKIE[$uriSegments3])){
  // load content-deluxe-cookie-tracking.php
          get_template_part( 'partners/content', 'deluxe-cookie-tracking' );
      } 
  ?&gt;
  </code></pre>
  
  <p>content-deluxe-cookie-tracking.php</p>
  
  <pre><code>&lt;input type="hidden" id="1234567890" name="1234567890" value="&lt;?php echo $uriSegments3 = $_SESSION['uriName']; ?&gt;"&gt;
  </code></pre>
  
  <p>So far: </p>
  
  <ol>
  <li>I have been able to create a cookie that persists as long as the browser is open and expires when it closes - which is what I want.</li>
  <li>If the campaign URL is in the address bar then the "if" statement in <code>form-page.php</code> returns true, 
  <code>content-deluxe-cookie-tracking.php</code> loads and the input <code>value="&lt;?php echo $uriSegments3 = $_SESSION['uriName']; ?&gt;"</code> is the cookie name.</li>
  <li>BUT, if the campaign is on another page and the user gets to the form page where there is no campaign URL, then the "if" statement in <code>form-page.php</code> returns false, 
  <code>content-deluxe-cookie-tracking.php</code> doesn't load - and I have checked to make sure the cookie exists.</li>
  </ol>
  
  <p>So, for example:</p>
  
  <p>www.mysitename.com/form-page/?UTM_keyword=field%20service%20software_phrase&amp;UTM_source=Paid%20Search&amp;gclid=EAIaI</p>
  
  <p>works, but:</p>
  
  <p>www.mysitename.com/form-page/ </p>
  
  <p>doesn't work, even if the cookie gets set on another page, for example:</p>
  
  <p>www.mysitename.com/another-page/?UTM_keyword=field%20service%20software_phrase&amp;UTM_source=Paid%20Search&amp;gclid=EAIaI</p>