@tanglemedia/directus-extension-bundle-page-views
Version:
A Directus extension bundle that adds a page views collection and dashboard to track page views.
265 lines (258 loc) • 5.45 kB
JavaScript
const COLLECTION_NAME = "tngl_page_views";
const COLLECTION_INSERTS = [
{
accountability: null,
archive_app_filter: true,
archive_value: "archived",
collapse: "open",
collection: COLLECTION_NAME,
display_template: "{{visit_date}} {{page_url}}",
hidden: false,
icon: "remove_red_eye",
singleton: false,
translations: JSON.stringify([
{
language: "en-US",
translation: "Page Views",
},
{
language: "en-CA",
translation: "Page Views",
},
]),
unarchive_value: "draft",
versioning: false,
},
];
const FIELD_INSERTS = [
{
collection: COLLECTION_NAME,
field: "id",
hidden: true,
interface: "input",
readonly: true,
required: false,
sort: 1,
width: "full",
},
{
collection: COLLECTION_NAME,
field: "ip_address",
group: "connection",
hidden: false,
interface: "input",
readonly: true,
required: false,
sort: 1,
width: "full",
},
{
collection: COLLECTION_NAME,
field: "city",
group: "location",
hidden: false,
interface: "input",
readonly: true,
required: false,
sort: 1,
width: "half",
},
{
collection: COLLECTION_NAME,
display: "labels",
display_options: JSON.stringify({
choices: [
{
text: "desktop",
value: "desktop",
},
{
text: "mobile",
value: "mobile",
},
],
}),
field: "device",
group: "connection",
hidden: false,
interface: "select-dropdown",
note: "Type of device used when the page was viewed (e.g. desktop, mobile)",
options: JSON.stringify({
choices: [
{
text: "desktop",
value: "desktop",
},
{
text: "mobile",
value: "mobile",
},
],
}),
readonly: true,
required: false,
sort: 2,
width: "full",
},
{
collection: COLLECTION_NAME,
field: "country",
group: "location",
hidden: false,
interface: "input",
readonly: true,
required: false,
sort: 2,
width: "half",
},
{
collection: COLLECTION_NAME,
display: "datetime",
field: "visit_date",
hidden: false,
interface: "datetime",
note: "Date when the page was viewed",
options: JSON.stringify({
use24: false,
}),
readonly: true,
required: false,
sort: 2,
special: "date-created",
width: "full",
},
{
collection: COLLECTION_NAME,
display: "raw",
field: "geolocation",
group: "location",
hidden: false,
interface: "map",
options: JSON.stringify({
defaultView: {
zoom: 0.1429493193490147,
pitch: 0,
center: {
lat: 28.85018279638213,
lng: 24.752118478332022,
},
bearing: 0,
},
geometryType: "Point",
}),
readonly: true,
required: false,
sort: 3,
translations: JSON.stringify([
{
language: "en-CA",
translation: "Location on the map",
},
{
language: "en-US",
translation: "Location on the map",
},
]),
width: "full",
},
{
collection: COLLECTION_NAME,
display: "raw",
field: "platform",
group: "connection",
hidden: false,
interface: "input",
note: "Type of browser used when the page was viewed",
readonly: true,
required: false,
sort: 3,
width: "full",
},
{
collection: COLLECTION_NAME,
field: "page_title",
hidden: false,
interface: "input",
readonly: true,
required: false,
sort: 3,
width: "full",
},
{
collection: COLLECTION_NAME,
display_options: JSON.stringify({
showCopy: true,
showLink: true,
}),
field: "page_url",
hidden: false,
interface: "input",
readonly: true,
required: false,
sort: 4,
width: "full",
},
{
collection: COLLECTION_NAME,
field: "connection",
special: "alias,no-data,group",
interface: "group-detail",
readonly: false,
hidden: false,
sort: 5,
width: "full",
translations: JSON.stringify([
{
language: "en-CA",
translation: "How did this visitor view the page?",
},
{
language: "en-US",
translation: "How did this visitor view the page?",
},
]),
required: false,
},
{
collection: COLLECTION_NAME,
field: "location",
special: "alias,no-data,group",
interface: "group-detail",
readonly: false,
hidden: false,
sort: 6,
width: "full",
translations: JSON.stringify([
{
language: "en-CA",
translation: "Where did this visitor come from?",
},
{
language: "en-US",
translation: "Where did this visitor come from?",
},
]),
required: false,
},
];
export async function up(knex) {
// if tngl_page_views exists in directus_collections table, do not proceed with migration
const collectionExists = await knex("directus_collections")
.select("collection")
.where("collection", COLLECTION_NAME)
.first();
// register tngl_page_views table into `directus_collections` table
// for full list of directus_collections properties, refer to: https://docs.directus.io/reference/system/collections.html
if (!collectionExists) {
await knex("directus_collections").insert(COLLECTION_INSERTS);
}
// register tngl_page_views fields into `directus_fields` table
// for full list of directus_fields properties, refer to: https://docs.directus.io/reference/system/fields.html
await knex("directus_fields").insert(FIELD_INSERTS);
}
export async function down(knex) {
// remove associated data from directus_fields table
await knex("directus_fields").where("collection", COLLECTION_NAME).del();
// remove associated data from directus_collections table
await knex("directus_collections").where("collection", COLLECTION_NAME).del();
}