highsolutions/google-keywords

一个用于与Google搜索控制台同步以获取访客搜索关键词的Laravel包

1.2.0 2020-09-09 13:12 UTC

This package is auto-updated.

Last update: 2024-09-09 22:26:11 UTC


README

一个用于与Google搜索控制台同步以获取访客搜索关键词的Laravel包。

Laravel-Poster by HighSolutions

安装

将以下行添加到Laravel webapp的composer.json文件的require部分

    "require": {
        "HighSolutions/GoogleKeywords": "1.*"
    }

运行composer update以安装此包。

此包使用Laravel 5.5包自动发现。对于之前的Laravel版本,您需要通过添加服务提供程序条目来更新config/app.php

'providers' => [
    // ...
    HighSolutions\GoogleKeywords\GoogleKeywordsServiceProvider::class,
];

接下来,发布所有包资源

    php artisan vendor:publish --provider="HighSolutions\GoogleKeywords\GoogleKeywordsServiceProvider"

这将添加到您的项目中

- migration - database table for storing keywords
- configuration - package configurations

请记住运行迁移

    php artisan migrate

下一步是通过调度程序(app\Console\Kernel.php)添加cron任务

    protected function schedule(Schedule $schedule)
    {
    	// ...
        $schedule->command('keywords:fetch')->daily();
    }

配置

网站在配置中定义如下

<?php

return [
    'websites' => [
    	[
    		'url' => 'http://highsolutions.pl',
    		'credentials' => storage_path('app/HS-Credentials.json'),
    	],
    	// ...
    ],
];

无效的配置将返回错误信息。

Google搜索控制台和Google云平台集成

为了获取Google搜索的关键词,您需要完成以下2个步骤

  1. Google搜索控制台(https://www.google.com/webmasters/tools/home
  • 添加您的网站
  • 验证您对网站的拥有权
  1. Google云平台(https://console.cloud.google.com/apis/credentials

模型结构

模型包含以下字段

  • id - 主键
  • url - 网站URL
  • keyword - 完整关键词
  • date - 结果日期
  • clicks - 搜索视图中特定关键词链接的点击次数
  • impressions - 搜索视图中特定关键词链接的显示次数
  • ctr - 点击率(点击次数/显示次数)
  • avg_position - 搜索视图中特定关键词链接的平均位置

模型API

为了更轻松地使用收集到的数据,存在一个简单的API,用于大多数常见用例

  • url('http://example.com') - 添加where子句,仅将结果限制为单个网站(在仅获取单个网站时不是必需的)
  • grouped() - 准备每个关键词的点击次数和显示次数的总和,这些关键词是为网站收集的
  • byDay() - 准备每天点击次数和显示次数的总和
  • orderByDate($dir = 'asc') - 按日期排序结果
  • orderBySum($param = 'clicks') - 按点击次数/显示次数的总和排序结果(与grouped作用域一起使用)
  • orderByAlpha($dir = 'asc') - 按字母顺序排序

用法

  1. 获取您网站的最受欢迎的关键词
<?php
	use HighSolutions\GoogleKeywords\Models\GoogleKeyword;

	$results = GoogleKeyword::url('http://highsolutions.pl')->grouped()->orderBySum('clicks')->take(10)->get();
  1. 按日获取存储的所有关键词
<?php
	use HighSolutions\GoogleKeywords\Models\GoogleKeyword;

	$results = GoogleKeyword::url('http://highsolutions.pl')->orderByDate()->get();
  1. 获取上个月的点击/显示次数
<?php
	use Carbon\Carbon;
	use HighSolutions\GoogleKeywords\Models\GoogleKeyword;

	$results = GoogleKeyword::url('http://highsolutions.pl')->byDate()->where('date', '>=', Carbon::now()->subMonth(1))->orderByDate()->get();

变更日志

1.2.0

  • Laravel 7.x和8.x支持

1.1.0

  • Laravel 6.0 支持

1.0.3

  • 支持包自动发现

1.0.2

  • 修复检查配置的错误

1.0.0

  • 创建包
  • 模型API

路线图

  • 同时收集子站点的信息
  • 作为控制器和视图的用法示例
  • 注释
  • 单元测试!

鸣谢

本包由 HighSolutions 开发,这是一家来自波兰的软件公司,热爱 Laravel。