Home > HowTo, Linux > bash get analytics data

bash get analytics data

December 17th, 2013 Leave a comment Go to comments

This is a bash script to get Google Analytics Data, in this case A total day Pageviews.

Here is the code:


#!/bin/bash
# Get  google anlytics pageview results
# by Felipe Ferreira Dec 17 2013
# v1

#todo: get REAL TIME data


EMAIL="fel@in.com"
PASS="123123123"


START_DATE=`date --date "2 day ago" +%Y-%m-%d`
END_DATE=`date --date "1 day ago" +%Y-%m-%d`

TOKEN=`curl https://www.google.com/accounts/ClientLogin -s -d Email=$EMAIL -d Passwd=$PASS -d accountType=GOOGLE -d source=curl-accountFeed-v1 -d service=analytics | grep "Auth=" | cut -d"=" -f2`
#echo "TOKEN: $TOKEN"

function URL {
PROFILE_ID=$1
SITE=$2

feedUri="https://www.google.com/analytics/feeds/data\
?start-date=$START_DATE\
&end-date=$END_DATE\
&dimensions=ga:source,ga:medium\
&metrics=ga:pageviews\
&sort=-ga:pageviews\
&max-results=100\
&ids=ga:$PROFILE_ID\
&prettyprint=true"

#echo -e "\n\n$feedUri\n\n"
curl $feedUri -o ga.xml -s --header "Authorization: GoogleLogin auth=$TOKEN"
cat ga.xml
TOTAL_PAGEVIEWS=`grep -A 1 '' ga.xml  |grep value |awk -F"=" '{ print $NF }' |sed  -e 's/"//g' -e 's/\///g' -e 's/>//g'`
#rm -f ga.xml
echo -e "\nTotal Pageviews do $SITE ( $START_DATE - $END_DATE ) \n${TOTAL_PAGEVIEWS}\n"
}

URL 2723201 mengo

echo "Done"
exit 0


Categories: HowTo, Linux Tags: , , , , , ,
  1. No comments yet.
  1. No trackbacks yet.

VAMOVE *

*