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();
        }

No comments:

Post a Comment