Google Analytics SEO - PHP API - php

I have a PHP file which give's me google analytics data, such as pageviews, Top Pages, or Organic Data simple stuff.
Now I neet to get Stuff from the SEO Part.
For example: TOP 50 Search Keywords (with Impression and Clicks)
I can't find any help in the API how to get these values.
this is a example of my api call:
$params = array(
'dimensions' => array('date', 'pagePath', 'pageTitle'),
'metrics' => array('sessions', 'pageviews'),
'sort' => '-ga:sessions',
'filters' => null,
'startdate' => $startdate,
'enddate' => $enddate,
'startindex' => null,
'limit' => 25,
'mapping' => array('pagepath' => 'pagepath', 'pagetitle' => 'pagetitle', 'sessions' => 'visits', 'pageviews' => 'pageviews'),
);
$results = $this->service->data_ga->get($this->profile, $params['startdate'], $params['enddate'], $metrics, $optParams);

The search engine optimization data shown in Google Analytics actually comes from Google Search Console (Webmaster Tools) and is available from the Google Search Console API.
https://developers.google.com/webmaster-tools/?hl=en

You would need to update the Dimensions and Metrics of your query. The traffic dimensions and metrics should be of help.
Below is a simplified query which gets the number of impressions and clicks for various sources and keyword combinations:
$optParams = array(
'dimensions' => 'ga:source,ga:keyword',
'sort' => '-ga:impressions,ga:source',
'filters' => 'ga:medium==organic',
'max-results' => '25');
$metrics = 'ga:impressions,ga:adClicks';
$results = $this->service->data_ga->get(
'ga:XXXX',
'today',
'7daysAgo',
$metrics,
$optParams);
Your code must have some mapping that it does from the values in $param field to the actual dimensions and metric names. I would also encourage you to play around with the query explorer to get a field of what queries are possible.

Related

Google analytics get the total pageviews of one URL plus same URLS with get parameters

I am currently getting my data like this:
$result = Analytics::performQuery(
Period::days(7),
'ga:pageviews',
[
'dimensions' => 'ga:pagePath',
'max-results' => $maxResults,
'sort' => '-ga:pageviews',
'filters' => 'ga:pagePath=~/blog*'
]
);
The response delivers multiple pages with the same slug like:
/blogs/article1
/blogs/article1?ref=homepage
and so on.
In order to get all the pageviews I currently explode the URL at the get parameter then in a foreach loop I get the total pageviews like this:
$article = Analytics::performQuery(
Period::days(7),
'ga:pageviews',
[
'dimensions' => 'ga:pagePath',
'sort' => '-ga:pageviews',
'filters' => 'ga:pagePath=='.$result['url']
]
);
then I get the total pageviews with article['totalsForAllResults']['ga:pageviews']
This obviously ends up making many google requests, which have a daily limit. Is there a way to get the total pageviews of one URL plus all the URLs that have the same name and get parameters attached?
To get the total number of pageviews (intended as a single number) just don't define the dimension 'dimensions' => 'ga: pagePath' in the request.

google analytics api data does not match to the actual data on my account

I am using the google analytics api in php. When I want to get the details of event levels like total events and unique events it gives inaccurate values. When I use my google analytics account (website) I get the right values. This is the code I use to retrieve the events:
$optParams = array(
'dimensions' => 'ga:eventcategory,ga:eventaction,'.
'ga:eventlabel,ga:source,ga:devicecategory',
'filters' => 'ga:eventcategory==category name,'.
'ga:eventaction==event name',
'start-index' => $start_index,
'max-results' => $limit
);
$results = $analytics->data_ga->get(
'ga:' . $this->ga_api_profileId,
$start_date,
$end_date,
'ga:totalEvents,ga:uniqueEvents',
$optParams
);

Create campaign with dynamic segment using MailChimp API V3.0

Using MailChimp API V3.0 to create a campaign.
I want to create a campaign that sends to users with a specific interest. It looks like this is possible in the docs, but I've tried every permutation I can think of. I can create the campaign fine as long as I leave out the segment_ops member. Does anyone have an example of PHP code that will do it?
It seems that interests are handled strangely since you don't include the interest-category when setting a users interests via the API. I'm not sure how this affects campaign creation.
I've gotten this to work, API definition can be found here https://us1.api.mailchimp.com/schema/3.0/Segments/Merge/InterestSegment.json
Interests have to be grouped under interest categories (called 'Groups' in some parts of the UI).
Here is the JSON for the segment_opts member of the recipients array:
"segment_opts": {
"match": "any",
"conditions": [{
"condition_type": "Interests",
"field": "interests-31f7aec0ec",
"op": "interestcontains",
"value": ["a9014571b8", "5e824ac953"]
}]
}
Here is the PHP array version with comments. The 'match' member refers to the the rules in the array of 'conditions'. The segment can match any, all, or none of the conditions. This example has only one condition, but others can be added as additional arrays in the 'conditions' array:
$segment_opts = array(
'match' => 'any', // or 'all' or 'none'
'conditions' => array (
array(
'condition_type' => 'Interests', // note capital I
'field' => 'interests-31f7aec0ec', // ID of interest category
// This ID is tricky: it is
// the string "interests-" +
// the ID of interest category
// that you get from MailChimp
// API (31f7aec0ec)
'op' => 'interestcontains', // or interestcontainsall, interestcontainsnone
'value' => array (
'a9014571b8', // ID of interest in that category
'5e824ac953' // ID of another interest in that category
)
)
)
);
You may also send to a Saved segment. The gotcha on this is that the segment_id has to be int. I was saving this value in a db as varchar, and it would not work unless cast to int.
(i am using use \DrewM\MailChimp\MailChimp;)
$segment_id = (int) $methodThatGetsMySegmentID;
$campaign = $MailChimp->post("campaigns", [
'type' => 'regular',
'recipients' => array(
'list_id' => 'abc123yourListID',
'segment_opts' => array(
'saved_segment_id' => $segment_id,
),
),
'settings' => array(
'subject_line' => 'A New Article was Posted',
'from_name' => 'From Name',
'reply_to' => 'info#example.com',
'title' => 'New Article Notification'
)
]);

Translate value before sending information

is there a away to translate values in php using either google api translate or any other api...
<?php
// 1.- Query to get information
// 2.- build array with that query
// Example array from query
$data = array(
'0' => array (
'name' => 'Zapatos',
'color' => 'Verde'
),
'1' => array (
'name' => 'Casa',
'color' => 'Rosa'
),
);
// Now that the array has been build, lets make a translation
// Which I have no idea how to do that but the final array should be
$final = array(
'0' => array (
'name' => 'Zapatos',
'color' => 'Verde',
'name_en' => 'Shoes',
'color_en' => 'Green'
),
'1' => array (
'name' => 'Casa',
'color' => 'Rosa',
'name_en' => 'House',
'color_en' => 'Pink'
),
);
is this process possible or am I just dreaming?
I have very little knowledge on how exactly Goolge API works since I only use the Google Translate widget and the translation is after you present the information but in this case we need to make a translation before presenting the information...
Google translate API is a paid service. You need to get a api key from google api services :
google translate API
After that, you can make a curl to google api after getting your results from query :
sample url for curl :
https://www.googleapis.com/language/translate/v2?key=INSERT-YOUR-KEY&source=en&target=de&q=Hello%20world&q=My%20name%20is%20Jeff
You will get the results as JSON object,do json_decode and add results to your array.

Channel-wise Google adsense information

I am using adsense Management API and followed everything there to generate a demo report as it is described in the example on their site.
The information I am currently fetching uses this information :
$startDate = '2012-03-01';
$endDate = '2012-04-18';
$optParams = array(
'filter' => array(
'AD_CLIENT_ID==' . $adClientId
),
'metric' => array(
'CLICKS', 'PAGE_VIEWS','COST_PER_CLICK','EARNINGS'
),
'dimension' => array('DATE'),
'sort' => 'DATE'
);
What I have to ask is is it possible to arrange this data according to the channels So that I get channel-wise information on EARNING, COST_PER_CLICK and PAGE_VIEWS. And how?
Assuming you're talking about custom channels, all you have to do is add one of the following to your list of dimensions, depending on what you prefer:
CUSTOM_CHANNEL_NAME
CUSTOM_CHANNEL_ID
CUSTOM_CHANNEL_CODE
You can think of dimensions as groups, that is, what you're organising your data by.
You can check this blog post for some more info.

Categories