0

im trying to get this script running only if the session is not started yet. The page reload it self after the user click in some options. But the infor to google analytics must be send only the first time when the session start. The script is working but the analytics are wrong, 'cause it send several times the same session.

PHP SCRIPT:

if (session_id() === "") {
    echo time();
    session_start();

    include('ss-ga.class.php');

    $ssga = new ssga( 'UA-10536XXXX-1', 'www.site.com' );

    //Set a pageview
    $ssga->set_page( 'b1.php' );
    $ssga->set_page_title( 'b1' );

    // Send
    $ssga->send();
    $ssga->reset();

}

the IF(SESSION_ID==="") and if (session_status() == PHP_SESSION_NONE)

JonTargaryen
  • 1,089
  • 1
  • 6
  • 17
  • create a variable and do if(session_started != 'y') { session_start()...do stuff...session_started = 'y';} – clearshot66 Sep 15 '17 at 15:06
  • Possible duplicate of [Check if PHP session has already started](https://stackoverflow.com/questions/6249707/check-if-php-session-has-already-started) – Eric Sep 15 '17 at 15:14

1 Answers1

0

My suspicion - it's not clear from the documentation - that using session_id to read the session ID only works after a session_start call.

A better approach would be to set a session variable after you've first run the GA code. Once set, future pageviews would skip that code because the session value exists.

session_start();

if(!isset($_SESSION['analytics_sent'])) {
    echo time();

    include('ss-ga.class.php');

    $ssga = new ssga( 'UA-10536XXXX-1', 'www.site.com' );

    //Set a pageview
    $ssga->set_page( 'b1.php' );
    $ssga->set_page_title( 'b1' );

    // Send
    $ssga->send();
    $ssga->reset();

    $_SESSION['analytics_sent'] = true;
}
ceejayoz
  • 165,698
  • 38
  • 268
  • 341