Wednesday, January 2, 2013

Google Analytics - Finding Page Views and Visits of our site


/* Google Analytics Configuration - Starts */
    //Your GA Profile Password
    $pass_word = 'Password';
     //Your GA Profile Name
    $user_name = "username";
    // Your GA Profile ID
    $profile_id = "ga:12345678";

    // include the Google Analytics PHP class
    include_once "googleanalytics.class.php";

    // create an instance of the GoogleAnalytics class using your own Google {email} and {password}
    $ga = new GoogleAnalytics($user_name, $pass_word);

    // set the Google Analytics profile you want to access - format is 'ga:123456';
    $ga->setProfile($profile_id);
    /* Google Analytics Configuration - Ends */

    try {
            // set the date range we want for the report - format is YYYY-MM-DD
            $ga->setDateRange(date('Y-m-d',$from_date), date('Y-m-d',$to_date));

            $page_views = $ga->getReport(
                        array('dimensions'=>urlencode('ga:date,ga:country'),
                                'metrics'=>urlencode('ga:pageviews,ga:visits'),
                                'filters'=>urlencode('ga:country=@India'),
                                'sort'=>'-ga:pageviews'
                                )
                        );
        } catch (Exception $e) {
                print 'Error: ' . $e->getMessage();
        }

Weeks Inside a Quarter


/*
 * Weeks in Current Quarter
 */
We can get the Current Quarter starting date and ending date through this below link,
http://phpndrupal.blogspot.in/2013/01/current-quarter-of-year.html


$quarter_start_date = $quarter_date[0];
$quarter_end_date = $quarter_date[1];

function WeeksInsideQuarter($quarter_start_date, $quarter_end_date){
    $start_date = strtotime($quarter_start_date);
    $end_date = strtotime($quarter_end_date);
    for($i=1; $i<15; $i++, $start_date+=604800){
        $week_dates[$i] = $start_date;
    }
    return $week_dates;

Current Quarter of the Year


/*
 * To get Current Quarter of the Year
 */
function current_quarter($n){
     $currentQuarter = 0;
     if($n < 4){
          $currentQuarter = "01-01-".date('Y')." to 31-03-".date('Y')."";
     } elseif($n > 3 && $n <7){
          $currentQuarter = "01-04-".date('Y')." to 30-06-".date('Y')."";
     } elseif($n >6 && $n < 10){
          $currentQuarter = "01-07-".date('Y')." to 30-09-".date('Y')."";
     } elseif($n >9){
          $currentQuarter = "01-10-".date('Y')." to 31-12-".date('Y')."";
     }
     return $quarter = explode(" to ", $currentQuarter);
}

echo $quarter_date = current_quarter(date('n'));