Wrapping javascript code conditionally using a cookie
This StackOverflow question should contain enough information to solve your issue.

I would go with the one that suggests using setcookie(). $_SESSION information is cleared when the user's session ends, generally at the close of the browser. Here is a recent example of me using cookies to remember an anonymous user's submission of a Webform and redirect them to the completion page if they tried to visit the Webform again:

    &lt;?php
    define('REQUEST_TIME', $_SERVER['REQUEST_TIME']);

    function _mymodule_manage_cookie($nid, $type = 'my_cookie_type') {
      $matches = (bool) $nid == variable_get("mymodule_{$type}_webform_nid", FALSE);
      if (empty($_COOKIE["Drupal_visitor_mymodule_{$type}_submitted"]) &amp;&amp; $matches) {
        mymodule_cookie_save(array("mymodule_{$type}_submitted" =&gt; TRUE));
      }
    }

    /**
     * Drupal 7 yoink!
     */
    function mymodule_cookie_save(array $values) {
      foreach ($values as $field =&gt; $value) {
        // Set cookie for 365 days.
        setrawcookie('Drupal.visitor.' . $field, rawurlencode($value), REQUEST_TIME + 31536000, '/');
      }
    }

    function mymodule_cookie_delete($cookie_name) {
      setrawcookie('Drupal.visitor.' . $cookie_name, '', REQUEST_TIME - 3600, '/');
    }
    ?&gt;


You need to put this in a module or in template.php in your theme. You can conditionally include the code in a manner similar to the following:

template.php

    &lt;?php
    function THEMENAME_preprocess_page(&amp;$variables) {
      // THEMENAME_get_first_visit_cookie() would refer to a function
      // you have to write that retrieves if the cookie
      // is set. You could alternatively try just using
      // $_COOKIE['my_cookie_type'].
      if (THEMENAME_get_first_visit_cookie()) {
        $variables['first_visit'] = TRUE;
      }
    }
    ?&gt;


page.tpl.php

    (skip to wherever you want to embed the code)
    &lt;?php if ($first_viist): ?&gt;
      // Paste your code here
    &lt;?php endif; ?&gt;


Now, one more point. The way I just showed you isn't the best way. You should add your Javascript properly using drupal_add_js() in a module or by adding a separate Javascript file in the theme that reacts to a class on the &lt;body&gt; tag and displays your first visit message (or whatever you're doing). You would still need the $first_visit variable to determine whether to add the class or not.