Google analytics API query.

Discussion in 'PHP' started by 50inches, May 30, 2012.

  1. #1
    Hello,

    I just got started working with the google analytics api. This little function works nice for me to get the TOTAL amount of visits from 2012-03-03 to 2012-03-06.

    
    function getResults(&$analytics, $profileId) {
       return $analytics->data_ga->get(
           'ga:' . $profileId,
           '2012-03-03',
           '2012-03-06',
           'ga:visits');
    }
    
    PHP:
    However, I want the result to be returned in an array where I am presented with the amount of visits per day, for each of the days between the two dates.

    How can I achive this?

    Thanks in advance.
     
    50inches, May 30, 2012 IP
  2. ROOFIS

    ROOFIS Well-Known Member

    Messages:
    1,234
    Likes Received:
    30
    Best Answers:
    5
    Trophy Points:
    120
    #2
    if it's a string your getting try explode on the substring whitespaces to assemble values and loop though.
    eg:

    
    <?php
    $array = explode(" ", $your_input_string);
    
    //your array sorting/filtering code here
    
    print_r($array);
    ?>
    
    PHP:

    On the other hand you need to call the regex engine to process data on the fly there's preg_split too. This will explode on all character returns - whitespace, tabs, returns etc.

    (from doc's)
    eg:

    
    <?php
    $array = preg_split("/[\s,]+/", $your_input_string); //split string by any number of commas or space characters including " ", \r, \t, \n and \f
    
    //your array sorting/filtering code here
    
    print_r($array);
    ?>
    
    PHP:





    ROOFIS
    :cool:
     
    ROOFIS, May 30, 2012 IP
  3. 50inches

    50inches Greenhorn

    Messages:
    16
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    13
    #3
    Thank you for the reply, but the issue is not in how to explode strings, but rather that I want analytics to return all the visitor counts for EACH day. As I mentioned, right now it returns the TOTAL for all the days in between.

    So the question is, what do I need to query the API for, in order to get that returned from the analytics API.
     
    50inches, May 30, 2012 IP