Analytics.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. namespace app\models;
  3. use Yii;
  4. /**
  5. * This is the model class for table "analytics".
  6. *
  7. * @property int $id
  8. * @property string $key1 ключ1
  9. * @property string $key2 ключ2
  10. * @property string $date Дата
  11. * @property int|null $vald Значение цифровое
  12. * @property int|null $valt Значение текстовое
  13. * @property string|null $ext другие значения
  14. */
  15. class Analytics extends \yii\db\ActiveRecord
  16. {
  17. /**
  18. * @var string
  19. */
  20. private $_token = "y0_AgAAAAABspRzAAKENgAAAADl049iHuuiZPWuSTOGV3NsbVUNsXCymlc";
  21. private $_Yid = 34950850;
  22. private $_login = 'amicru';
  23. /**
  24. * @var array
  25. */
  26. private $context = array();
  27. /**
  28. * Constructor
  29. *
  30. * @param bool $use_only_cache
  31. */
  32. function __construct()
  33. {
  34. $opts = array(
  35. 'http'=>array(
  36. 'method'=>"GET",
  37. 'header'=>"Accept-language: ru\r\n" .
  38. 'Authorization: OAuth '.$this->_token."\r\n".
  39. 'Content-Type: application/x-yametrika+json'
  40. )
  41. );
  42. $this->context = stream_context_create($opts);
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public static function tableName()
  48. {
  49. return 'analytics';
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function rules()
  55. {
  56. return [
  57. [['key1', 'key2', 'date'], 'required'],
  58. [['date', 'ext'], 'safe'],
  59. [['vald', 'valt'], 'integer'],
  60. [['key1', 'key2'], 'string', 'max' => 64],
  61. ];
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function attributeLabels()
  67. {
  68. return [
  69. 'id' => 'ID',
  70. 'key1' => 'ключ1',
  71. 'key2' => 'ключ2',
  72. 'date' => 'Дата',
  73. 'vald' => 'Значение цифровое',
  74. 'valt' => 'Значение текстовое',
  75. 'ext' => 'другие значения',
  76. ];
  77. }
  78. public function YGetStat($start_date = NULL ,$now_date = NULL)
  79. {
  80. $id = $this->_Yid;
  81. $start_date = ( $start_date )?$start_date:date("Ym01");
  82. $now_date = ( $now_date )?$now_date:date("Ymd");
  83. $url = "https://api-metrika.yandex.ru/stat/v1/data?ids=" . $id . "&date1=" . $start_date . "&date2=" . $now_date . "&metrics=ym:s:visits,ym:s:pageviews,ym:s:users&direct_client_logins=".$this->_login."&oauth_token=" . $this->_token;
  84. $results_page = file_get_contents($url, false, $this->context);
  85. $Stat = json_decode($results_page);
  86. return array('ids'=>$Stat->query->ids[0],'visits'=>$Stat->totals[0],'pageviews'=>$Stat->totals[1],'users'=>$Stat->totals[2],'date1'=>$Stat->query->date1,'date2'=>$Stat->query->date2);
  87. }
  88. public function YGetFullStat($start_date = NULL ,$now_date = NULL)
  89. {
  90. $id = $this->_Yid;
  91. $start_date = ( $start_date )?$start_date:date("Ym01");
  92. $now_date = ( $now_date )?$now_date:date("Ymd");
  93. $url = "https://api-metrika.yandex.ru/stat/v1/data?ids=" . $id . "&date1=" . $start_date . "&date2=" . $now_date . "&metrics=ym:s:pageviews,ym:s:users,ym:s:newUsers,ym:s:bounceRate,ym:s:avgVisitDurationSeconds,ym:s:pageDepth&direct_client_logins=".$this->_login."&oauth_token=" . $this->_token;
  94. $results_page = file_get_contents($url, false, $this->context);
  95. $Stat = json_decode($results_page);
  96. return array('ids'=>$Stat->query->ids[0],'date1'=>$Stat->query->date1,'date2'=>$Stat->query->date2,'data'=>$Stat);
  97. }
  98. public function YGetSourceStat($start_date = NULL ,$now_date = NULL)
  99. {
  100. $id = $this->_Yid;
  101. $start_date = ( $start_date )?$start_date:date("Ym01");
  102. $now_date = ( $now_date )?$now_date:date("Ymd");
  103. $url = "https://api-metrika.yandex.ru/stat/v1/data?ids=" . $id . "&date1=" . $start_date . "&date2=" . $now_date . "&metrics=ym:s:users&dimensions=ym:s:lastTrafficSource&direct_client_logins=".$this->_login."&oauth_token=" . $this->_token;
  104. $results_page = file_get_contents($url, false, $this->context);
  105. $Stat = json_decode($results_page);
  106. return $Stat;
  107. }
  108. public function YGetRegionStat($start_date = NULL ,$now_date = NULL)
  109. {
  110. $id = $this->_Yid;
  111. $start_date = ( $start_date )?$start_date:date("Ym01");
  112. $now_date = ( $now_date )?$now_date:date("Ymd");
  113. $url = "https://api-metrika.yandex.ru/stat/v1/data?ids=" . $id . "&date1=" . $start_date . "&date2=" . $now_date . "&metrics=ym:s:users&filters=ym:s:regionAreaName=='Алтайский%20край'&direct_client_logins=".$this->_login."&oauth_token=" . $this->_token;
  114. $results_page = file_get_contents($url, false, $this->context);
  115. $Stat = json_decode($results_page);
  116. return $Stat;
  117. }
  118. public function YGetStatByDay($start_date = NULL ,$now_date = NULL)
  119. {
  120. $id = $this->_Yid;
  121. $start_date = ( $start_date )?$start_date:date("Ym01");
  122. $now_date = ( $now_date )?$now_date:date("Ymd");
  123. $url = "https://api-metrika.yandex.ru/stat/v1/data/bytime?ids=" . $id . "&date1=" . $start_date . "&date2=" . $now_date . "&group=day&metrics=ym:s:visits,ym:s:pageviews,ym:s:users&direct_client_logins=".$this->_login."&oauth_token=" . $this->_token;
  124. $results_page = file_get_contents($url, false, $this->context);
  125. $Stat = json_decode($results_page);
  126. $byDate = array();
  127. foreach ($Stat->time_intervals as $key=>$GetData) {
  128. $byDate[$key]['date'] = $GetData[0];
  129. }
  130. foreach ($Stat->data[0]->metrics[0] as $key=>$GetData) {
  131. $byDate[$key]['visits'] = $GetData;
  132. }
  133. foreach ($Stat->data[0]->metrics[1] as $key=>$GetData) {
  134. $byDate[$key]['pageviews'] = $GetData;
  135. }
  136. foreach ($Stat->data[0]->metrics[2] as $key=>$GetData) {
  137. $byDate[$key]['users'] = $GetData;
  138. }
  139. return array('ids'=>$Stat->query->ids[0],'visits'=>$Stat->totals[0][0],'pageviews'=>$Stat->totals[0][1],'users'=>$Stat->totals[0][2],'date1'=>$Stat->query->date1,'date2'=>$Stat->query->date2, 'data'=>$byDate);
  140. }
  141. public function YGetOrganic($start_date = NULL ,$now_date = NULL)
  142. {
  143. $id = $this->_Yid;
  144. $start_date = ( $start_date )?$start_date:date("Ym01");
  145. $now_date = ( $now_date )?$now_date:date("Ymd");
  146. $url = "https://api-metrika.yandex.ru/stat/v1/data?ids=" . $id . "&date1=" . $start_date . "&date2=" . $now_date . "&dimensions=ym:s:searchEngine&metrics=ym:s:users&filters=ym%3As%3AtrafficSource%3D%3D'organic'&direct_client_logins=".$this->_login."&oauth_token=" . $this->_token;
  147. $results_page = file_get_contents($url, false, $this->context);
  148. $Stat = json_decode($results_page);
  149. return array('ids'=>$Stat->query->ids[0],'users'=>$Stat->totals[0],'date1'=>$Stat->query->date1,'date2'=>$Stat->query->date2, 'data'=>$Stat->data);
  150. return $Stat;
  151. }
  152. public function YGetReferer($start_date = NULL ,$now_date = NULL)
  153. {
  154. $id = $this->_Yid;
  155. $start_date = ( $start_date )?$start_date:date("Ym01");
  156. $now_date = ( $now_date )?$now_date:date("Ymd");
  157. $url = "https://api-metrika.yandex.ru/stat/v1/data/bytime?ids=" . $id . "&date1=" . $start_date . "&date2=" . $now_date . "&group=month&top_keys=20&metrics=ym:s:users&dimensions=ym:s:refererDomain&sort=ym:s:users&limit=20&direct_client_logins=".$this->_login."&oauth_token=" . $this->_token;
  158. $results_page = file_get_contents($url, false, $this->context);
  159. $Stat = json_decode($results_page);
  160. return $Stat;
  161. }
  162. public function YGetRegions($start_date = NULL ,$now_date = NULL)
  163. {
  164. $id = $this->_Yid;
  165. $start_date = ( $start_date )?$start_date:date("Ym01");
  166. $now_date = ( $now_date )?$now_date:date("Ymd");
  167. $url = "https://api-metrika.yandex.ru/stat/v1/data/?ids=" . $id . "&date1=" . $start_date . "&date2=" . $now_date . "&group=month&preset=geo_country&filters=ym%3As%3AregionCountryName=='Россия'&metrics=ym%3As%3Ausers,ym:s:visits&sort=-ym%3As%3Ausers&limit=10&direct_client_logins=".$this->_login."&oauth_token=" . $this->_token;
  168. $results_page = file_get_contents($url, false, $this->context);
  169. $Stat = json_decode($results_page);
  170. return $Stat;
  171. }
  172. public function YGetByTime($params1)
  173. {
  174. $id = $this->_Yid;
  175. $url = 'https://api-metrika.yandex.ru/stat/v1/data/bytime';
  176. $params = array(
  177. 'ids' => $id,
  178. 'direct_client_logins' => $this->_login,
  179. );
  180. $params = array_merge($params,$params1);
  181. $sets = $url . '?' . http_build_query($params, '', '&');
  182. $results_page = @file_get_contents( $sets, false, $this->context );
  183. $Stat = json_decode($results_page);
  184. return $Stat;
  185. }
  186. public function GetLive($metrics, $type){
  187. $results_page = file_get_contents( urldecode($metrics).'&period=month&graph=csv', false);
  188. switch( $type ){
  189. case '2d':
  190. $liv_stat = $this->Live2js($results_page);
  191. break;
  192. case '3d':
  193. $liv_stat = $this->Live2js3d($results_page);
  194. break;
  195. }
  196. return $liv_stat;
  197. }
  198. public function Live2js( $str ){
  199. $m = array( "Янв","Фев","Мар","Апр","Май","Июн","Июл","Авг","Сен","Окт","Ноя","Дек");
  200. $a = explode( "\n", $str );
  201. $r = array();
  202. foreach( $a as $text ){
  203. $tmp = explode( ";", $text );
  204. if( isset($tmp[0]) ){
  205. if( preg_match_all("/\"(.+)\s(\d+)/", $tmp[0], $atmp, PREG_SET_ORDER) ){
  206. $x = array();
  207. $x[$atmp[0][2]+2000][array_search( $atmp[0][1], $m )+1]['count']=$tmp[1];
  208. $r[]=$x;
  209. }
  210. }
  211. }
  212. // print_r($r);
  213. return $r;
  214. }
  215. public function Live2js3d( $str ){
  216. $m = array( "Янв","Фев","Мар","Апр","Май","Июн","Июл","Авг","Сен","Окт","Ноя","Дек");
  217. $a = explode( "\n", $str );
  218. $title = explode( ";", $a[0] );
  219. $r = array();
  220. foreach( $a as $text ){
  221. $tmp = explode( ";", $text );
  222. if( isset($tmp[0]) ){
  223. if( preg_match_all("/\"(.+)\s(\d+)/", $tmp[0], $atmp, PREG_SET_ORDER) ){
  224. for( $i=1 ; $i < count($tmp); $i++){
  225. $mm = array_search( $atmp[0][1], $m )+1;
  226. $r[$atmp[0][2]+2000][$mm][$i]=$tmp[$i];
  227. }
  228. }
  229. }
  230. }
  231. return $r;
  232. }
  233. public function GetYglob( $sstart, $send ){
  234. $sweek = array();
  235. $params = array(
  236. 'metrics' => 'ym:s:pageviews,ym:s:users,ym:s:newUsers,ym:s:bounceRate,ym:s:avgVisitDurationSeconds,ym:s:pageDepth',
  237. 'date1' => $sstart,
  238. 'date2' => $send,
  239. 'group' => 'month',
  240. 'top_keys' => 12,
  241. 'sort' =>'-ym:s:users'
  242. );
  243. $sweek = $this->YGetByTime($params);
  244. return $sweek;
  245. }
  246. public function GetRegionsSite( $date = '' ){
  247. if( $date == '' ) $date=date("Y-m-01");
  248. $RegionsSite = Analytics::find()
  249. ->where(['key1'=>'RegionsSite','key2'=>'', 'date'=>$date])
  250. ->one();
  251. if( !$RegionsSite ){
  252. $aregm = [];
  253. $str = @file_get_contents('/home/admin/import/livereg.csv');
  254. $aregms = explode("\n", $str);
  255. foreach( $aregms as $item ){
  256. $a = explode(';', $item);
  257. $aregm[str_replace( '"', '', $a[0])] = $a;
  258. }
  259. if( count($aregm) ){
  260. $this->setAttributes( ['key1'=>'RegionsSite', 'key2'=>'', 'date'=>date("Y-m-01"), 'ext'=>json_encode($aregm)] );
  261. $this->save(false);
  262. }
  263. }else{
  264. $aregm = json_decode($RegionsSite['ext']);
  265. }
  266. return $aregm;
  267. }
  268. }