天天查看GA的网页报告是件非常BORING的事,有没有什么办法可以自动将每天发过来的GA REPORT EMAIL ATTACHMENT自动导入到MYSQL数据库然后用自己网页数据库系统来查看呢?
当然可以,思路如下:
* 准备一个Linux的VPS或Cloud Server;
* 安装fetchmail,每隔一定时间自动检测Gmail,看有没有邮件过来:
/root/.fetchmailrc
set postmaster "user5"
set daemon 600
poll imap.gmail.com proto imap
user '[email protected]' there with password 'YYYYYY' is root here options ssl
mda '/usr/bin/procmail -d %
* 安装procmail和munpack,自动对收到的邮件分拣:
/root/.procmailrc
:0
*^content-Type:
{
:0c:
$HOME/mail_backup
METAMAIL_TMPDIR=$HOME/attachments
# forward to munpack
:0fw
* ^To.*XXXXXX*
| munpack -C /root; /root/importcsv XXXXX
}
* 关键的 Importcsv代码:
/root/importcsv
#!/bin/sh
cd /root
rm -f Report.csv*
unzip -o /root/Report.zip -d /root
rm -f /root/Report.zip*
rm -f data.csv
iconv -f UTF16 -t UTF8 Report.csv > data.csv
rm -f Report.csv*
lastdate=`date --date="yesterday" +"%Y-%m-%d"`
mysql -uroot -pZZZZZZ ga <<EOF
load data local infile './data.csv' into table report CHARACTER SET utf8 fields terminated by '\t' lines terminated by '\n' Ignore 1 lines ( url, pageviews, clicks, ctr, cpc, rpm, earnings ) SET account = '$1', date = '$lastdate';
EOF
* 数据库记录:
CREATE TABLE `report` (
`id` int(11) NOT NULL auto_increment,
`account` varchar(24) default NULL,
`date` varchar(10) default NULL,
`url` varchar(64) default NULL,
`pageviews` int(11) default NULL,
`clicks` int(11) default NULL,
`ctr` decimal(11,2) default NULL,
`cpc` decimal(11,2) default NULL,
`rpm` decimal(11,2) default NULL,
`earnings` decimal(11,2) default NULL,
`note` varchar(64) default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1 PACK_KEYS=0;
大概思路是这样,具体代码需要自己微调一下,希望对大家有用。
|