Page Visits Counter avatar
Page Visits Counter
Try for free

No credit card required

View all Actors
Page Visits Counter

Page Visits Counter

juansgaitan/counter
Try for free

No credit card required

Counts the current page visits inside an act.

Dockerfile

1# This is a template for a Dockerfile used to run acts in Actor system.
2# The base image name below is set during the act build, based on user settings.
3# IMPORTANT: The base image must set a correct working directory, such as /usr/src/app or /home/user
4FROM apify/actor-node-puppeteer-chrome:15-10.1.0
5
6# Second, copy just package.json and package-lock.json since it should be
7# the only file that affects "npm install" in the next step, to speed up the build
8COPY package*.json ./
9
10# Install NPM packages, skip optional and development dependencies to
11# keep the image small. Avoid logging too much and print the dependency
12# tree for debugging
13RUN npm --quiet set progress=false \
14 && npm install --only=prod --no-optional \
15 && echo "Installed NPM packages:" \
16 && (npm list --all || true) \
17 && echo "Node.js version:" \
18 && node --version \
19 && echo "NPM version:" \
20 && npm --version
21
22# Copy source code to container
23# Do this in the last step, to have fast build if only the source code changed
24COPY  . ./
25
26# NOTE: The CMD is already defined by the base image.
27# Uncomment this for local node inspector debugging:
28# CMD [ "node", "--inspect=0.0.0.0:9229", "main.js" ]

main.js

1const Apify = require('apify');
2
3const { log } = console;
4
5Apify.main(async () => {
6  const input = await Apify.getValue('INPUT');
7
8  const { client } = Apify;
9  const { keyValueStores } = client;
10  const {
11    count, limit, actName, email
12  } = input;
13  if (!count || !limit || !actName || !email) {
14    throw new Error('Missing input value.');
15  }
16  const { id: storeId } = await keyValueStores.getOrCreateStore({
17    storeName: actName
18  });
19  client.setOptions({ storeId });
20
21  const record = await keyValueStores.getRecord({ key: 'STATE' });
22
23  const storeRecord = record && record.body ? record.body : {};
24
25  const previousState = typeof storeRecord === 'string' ?
26    JSON.parse(storeRecord) : storeRecord;
27  log('Previous STATE:', previousState);
28
29  const currentCount = Number(count) || 0;
30  const previousCount = Number(previousState.count) || 0;
31  const nextCount = previousCount + currentCount;
32
33  if (nextCount > Number(limit)) {
34    const text = `
35      The ${actName} has reached its current page visits limit.
36
37      Current limit: ${limit},
38      Current count: ${nextCount}
39
40      Please notify the user to updgrade its current plan!
41    `;
42    log('Sending notification email...');
43    await Apify.call('apify/send-mail', {
44      to: email,
45      subject: `The ${actName} act has reached its limit!`,
46      text
47    });
48  }
49
50  const nextState = Object.assign({}, { count: nextCount, limit });
51  log('Next STATE:', nextState);
52
53  await keyValueStores.putRecord({
54    key: 'STATE',
55    body: JSON.stringify(nextState)
56  });
57});

package.json

1{
2    "name": "apify-project",
3    "version": "0.0.1",
4    "description": "",
5    "author": "It's not you it's me",
6    "license": "ISC",
7    "dependencies": {
8        "apify": "0.16.0",
9        "underscore": "latest"
10    },
11    "scripts": {
12        "start": "node main.js"
13    }
14}
Developer
Maintained by Community
Actor metrics
  • 1 monthly users
  • 0.0 days response time
  • Created in Mar 2018
  • Modified over 1 year ago
Categories