1

I cannot seem to parse out the metrics for an organic keyword. I am using simplexml_load_string to load the whole response string and then when I print_r the object I can see this:

[0] => SimpleXMLElement Object
                (
                    [id] => http://www.google.com/analytics/feeds/data?ids=ga:480&ga:keyword=cats&start-date=2011-04-10&end-date=2011-07-24
                    [updated] => 2011-07-23T17:00:00.001-07:00
                    [title] => ga:keyword=cats
                    [link] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [rel] => alternate
                                    [type] => text/html
                                    [href] => http://www.google.com/analytics
                                )

                        )

                )

But when I look at the actual (pre-formatted) response I see some extra data that didn't survive the format:

<entry gd:etag="W/&quot;hdSFEs.&quot;" gd:kind="analytics#datarow"> 
<id>http://www.google.com/analytics/feeds/data?ids=ga:430&amp;ga:keyword=cats&amp;start-date=2011-04-10&amp;end-date=2011-07-24</id> 
<updated>2011-07-23T17:00:00.001-07:00</updated> 
<title>ga:keyword=cats</title> 
<link rel="alternate" type="text/html" href="http://www.google.com/analytics"/> 
<dxp:dimension name="ga:keyword" value="cats"/> 
<dxp:metric confidenceInterval="0.0" name="ga:organicSearches" type="integer" value="2"/> 
</entry> 

I need that metric value at the bottom, in this case it's two. How can I get it?

Thanks!

Doug Molineux
  • 12,283
  • 25
  • 92
  • 144

2 Answers2

3

Edit: Arend is right, the problem is just SimpleXML handling the namespaces strangely.

You might need to pass the entire XML response to the SimpleXMLElement() constructor, not just the snippet you provided here. In particular you need the <feed> element because it has the namespace declarations:

<feed xmlns='http://www.w3.org/2005/Atom' xmlns:dxp='http://schemas.google.com/analytics/2009' xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/' xmlns:gd='http://schemas.google.com/g/2005' gd:etag='W/&quot;DUINSHcycSp7I2A9WxRWFEQ.&quot;' gd:kind='analytics#data'>
    <entry gd:etag="W/&quot;hdSFEs.&quot;" gd:kind="analytics#datarow"> 
    <id>http://www.google.com/analytics/feeds/data?ids=ga:430&amp;ga:keyword=cats&amp;start-date=2011-04-10&amp;end-date=2011-07-24</id> 
    <updated>2011-07-23T17:00:00.001-07:00</updated> 
    <title>ga:keyword=cats</title> 
    <link rel="alternate" type="text/html" href="http://www.google.com/analytics"/> 
    <dxp:dimension name="ga:keyword" value="cats"/> 
    <dxp:metric confidenceInterval="0.0" name="ga:organicSearches" type="integer" value="2"/> 
    </entry>
</feed>

Without these namespace declarations it looks like your XML library is dropping any namespaced attributes or elements (such as <dxp:metric>).

You might also get different results from a different version of the XML library. On my system your snippet produces a SimpleXMLElement with all of the attributes and elements, including <metric>, though I do get a bunch of PHP warnings complaining about the missing namespace declarations.

squirrel
  • 2,010
  • 14
  • 10
1

Edit: the easy way out is this:

var_dump($object->xpath('dxp:metric'));

The problem is that simplexml has a 'gotcha' with namespaces. As kudo's to squirrel, you indeed need to use namespaces, but I have a hunch that you only posted a part of the xml response and not the whole response.

There is an excellent post about it here: http://blogs.sitepoint.com/simplexml-and-namespaces/

In order to fetch the value of the metric (and the metric itself), you can get started with this piece of code.

var_dump($object->children('http://schemas.google.com/analytics/2009'));
Be aware that there are some attributes in there that are again in a different namespace.

The rest also has been answered here: Parse XML with Namespace using SimpleXML

Community
  • 1
  • 1
Arend
  • 3,741
  • 2
  • 27
  • 37