Guess.js for non-Webpack sites.
Automatic, dynamic, intelligent prefetching for faster page loads.
This directory uses Google Analytics data to determine which page a user is mostly likely to visit next from a given page (generatePredictions.js).
A client-side script (which you'll add to your application) sends a request to the server you are running to get the URL of the page it should fetch, it then prefetches this resource (script.js & server.js).
If a user is on a poor connection, prefetching will only occur if there's a high level of certainty that a user will go to a particular page next. If a client is using the Save-Data header, no prefetching will occur.
After downloading the Guess.js repo, cd to this directory and install the dependencies:
$ cd experiments/guess-static-sites
$ npm install
Use this command to run the server:
$ node server.js
'http://YOUR_SERVER_ENDPOINT/'
with the server endpoint you'll be using.Prerequisites:
This is the final, and lengthiest, part of the setup process - but it should only take about 5-10 minutes to complete if you already have Mongo and a Google Analytics account. Afterwards you'll be ready to consume and analyze your Google Analytics data.
Go to the Credentials page in the Google APIs console.
If you don't have an existing Google Cloud project, click "Create" to create a new project. Otherwise, use the dropdown in the upper left corner to select the existing project that you'd like to use.
Select "Service Account key" from the "Create credentials" dropdown.
Fill out the form for creating a service account key:
Your private key should have started downloading when you clicked the "Create" button for creating your service account.
$ openssl pkcs12 -in *.p12 -out key.pem -nodes -clcerts
The service account that you just created needs to be added as a user to your Google analytics account.
Note: A service account can only be associated with one Google Analytics account at a time. Thus, if you want to use predictive fetching on multiple sites, you'll need to create a separate service account for each.
You can enable this here.
This file will hold your confidential configuration details.
$ touch .env
Your file should look like this (replace with your own values):
VIEW_ID=12345678
SERVICE_ACCOUNT_EMAIL=example@example-project-123456.iam.gserviceaccount.com
To find your view ID, go to Google Analytics. Click the accounts dropdown (it's located in the upper lefthand corner of the screen, right next to the Google Analytics logo). The dialog that opens will have three columns: Analytics Accounts, Properties & Apps, & Views. The far right column ("Views") will contain the View ID for your site.
If mongod is not running, start it:
$ mongod
$ node generatePredictions.js
If this is successful, you should see something like this in the console for each page:
{ pagePath: 'dogs/poodle.html',
pageviews: 300,
exits: 200,
nextPageviews: 100,
nextExits: 100,
nextPages:
[ { pagePath: '/dogs/puppies/', pageviews: 100 } ],
percentExits: 0.6666666666666666,
topNextPageProbability: 0.3333333333333333 }
You can also explore the results in Mongo:
$ mongo
$ use guessjs_dev
$ db.predictions.find()
It is recommended that you set up a cron job to periodically re-run generatePredictions.js. This will ensure that the prefetch links are as accurate as possible. The ideal frequency of this cron job depends on how frequently your site changes.
You can also experiment with the date range of data that is used to generate predictions. By default, GuessJS uses the last 30 days of traffic to generate predictions, but this value can be changed (this is located in file: src/queryParams.js). It's important to have a sufficiently large data set, so the ideal date range will largely depend on a site's traffic volume. A very high-traffic site might find that using the last 1-7 days of traffic works best, while a low-traffic site find that using the last 30 days works best.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。