### Start Server Source: https://github.com/plausible/analytics/blob/master/CONTRIBUTING.md Starts the Plausible development server. ```Shell make server ``` -------------------------------- ### Setup Assets Source: https://github.com/plausible/analytics/blob/master/CONTRIBUTING.md Installs Tailwind CSS and Esbuild for asset management and processing. ```Shell mix assets.setup ``` -------------------------------- ### Start Phoenix Server Source: https://github.com/plausible/analytics/blob/master/CONTRIBUTING.md Starts the Phoenix development server to run the Plausible application. ```Shell mix phx.server ``` -------------------------------- ### Start Clickhouse Container Source: https://github.com/plausible/analytics/blob/master/CONTRIBUTING.md Starts the ClickHouse database container using Docker. ```Shell make clickhouse ``` -------------------------------- ### Start Postgres Container Source: https://github.com/plausible/analytics/blob/master/CONTRIBUTING.md Starts the PostgreSQL database container using Docker. ```Shell make postgres ``` -------------------------------- ### Install All Dependencies Source: https://github.com/plausible/analytics/blob/master/CONTRIBUTING.md Installs all project dependencies, including Elixir and Node.js packages, and sets up the environment. ```Shell make install ``` -------------------------------- ### Plausible CE Installation Instructions Source: https://github.com/plausible/analytics/blob/master/README.md Provides instructions for installing Plausible Community Edition (CE) on your own server. This is a community-supported project, and users are responsible for managing their infrastructure. ```Bash git clone https://github.com/plausible/community-edition.git cd community-edition # Further installation steps would follow here, likely involving Docker or specific commands. ``` -------------------------------- ### Install Tracker Dependencies Source: https://github.com/plausible/analytics/blob/master/CONTRIBUTING.md Installs the required dependencies for the Plausible tracker using npm. ```Shell npm ci --prefix tracker ``` -------------------------------- ### Install Dependencies (Bash) Source: https://github.com/plausible/analytics/blob/master/tracker/README.md Installs project dependencies using npm and downloads browsers required by Playwright for testing. ```bash npm install npx playwright install # installs browsers used by playwright ``` -------------------------------- ### Install Client-Side Dependencies Source: https://github.com/plausible/analytics/blob/master/CONTRIBUTING.md Installs the required client-side dependencies for the project's assets using npm. ```Shell npm ci --prefix assets ``` -------------------------------- ### Initialize Plausible Tracker Source: https://github.com/plausible/analytics/blob/master/tracker/npm_package/README.md Initializes the Plausible Analytics tracker with the required domain. This setup is necessary before any tracking events can be sent. ```javascript import { init } from '@plausible-analytics/tracker' init({ domain: 'my-app.com' }) ``` -------------------------------- ### Install Plausible Analytics Tracker Source: https://github.com/plausible/analytics/blob/master/tracker/npm_package/README.md Installs the Plausible Analytics tracker package using npm. This is the first step to integrating Plausible Analytics into your project. ```bash npm install @plausible-analytics/tracker ``` -------------------------------- ### Install Pre-commit Hooks Source: https://github.com/plausible/analytics/blob/master/CONTRIBUTING.md Installs pre-commit hooks for managing code quality and consistency across Elixir, JavaScript, and CSS. ```Shell pip install --user pre-commit pre-commit install ``` -------------------------------- ### Install Elixir Dependencies Source: https://github.com/plausible/analytics/blob/master/CONTRIBUTING.md Downloads the required Elixir dependencies for the project using Mix, the build tool for Elixir. ```Shell mix deps.get ``` -------------------------------- ### Initialize and Inject plausible-web.js Source: https://github.com/plausible/analytics/blob/master/tracker/test/fixtures/file-download-plausible-web.html This snippet demonstrates how to initialize Plausible Analytics with custom configuration and inject the tracking script into the webpage. It sets up the global `plausible` function and its queue, defines an initialization function, and then creates and appends the script tag with the necessary configuration. ```javascript window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) }; window.plausible.init = function(overrides) { window.plausible.o = overrides || {} }; var config = { domain: 'example.com' }; const script = document.createElement('script'); script.src = `/tracker/js/plausible-web.js?script_config=${encodeURIComponent(JSON.stringify(config))}`; var r = document.getElementsByTagName("script")[0]; r.parentNode.insertBefore(script, r); ``` -------------------------------- ### Seed Database Source: https://github.com/plausible/analytics/blob/master/CONTRIBUTING.md Populates the database with initial data, including creating a user account and a site with statistics. ```Shell mix run priv/repo/seeds.exs ``` -------------------------------- ### Create Databases Source: https://github.com/plausible/analytics/blob/master/CONTRIBUTING.md Creates the necessary databases in both PostgreSQL and ClickHouse using Ecto, the database wrapper for Elixir. ```Shell mix ecto.create ``` -------------------------------- ### Initialize Plausible Tracker with Custom Properties Source: https://github.com/plausible/analytics/blob/master/tracker/npm_package/README.md Initializes the Plausible Analytics tracker and configures custom properties to be sent with every page view. This allows for richer event data. ```javascript init({ domain: 'my-app.com', customProperties: { "content_category": "news" } }) ``` -------------------------------- ### Deploy Tracker Files Source: https://github.com/plausible/analytics/blob/master/CONTRIBUTING.md Generates tracker files in the specified directory using npm. ```Shell npm run deploy --prefix tracker ``` -------------------------------- ### Initialize Plausible Tracker with Dynamic Custom Properties Source: https://github.com/plausible/analytics/blob/master/tracker/npm_package/README.md Initializes the Plausible Analytics tracker with dynamic custom properties that are generated based on the current context, such as the document title. ```javascript init({ domain: 'my-app.com', customProperties: (eventName) => ({ "title": document.title }) }) ``` -------------------------------- ### Yalc Publish (Bash) Source: https://github.com/plausible/analytics/blob/master/tracker/README.md Publishes a local version of the npm package using Yalc for testing purposes. ```bash npm install yalc -g yalc publish ``` -------------------------------- ### Migrate Database Schema Source: https://github.com/plausible/analytics/blob/master/CONTRIBUTING.md Applies database migrations to build the schema for both PostgreSQL and ClickHouse using Ecto. ```Shell mix ecto.migrate ``` -------------------------------- ### JavaScript Tracker Entrypoint Source: https://github.com/plausible/analytics/blob/master/tracker/ARCHITECTURE.md The main entry point for the Plausible tracker script during compilation. This file orchestrates the initialization and functionality of the tracker, likely including event capturing and configuration loading. ```javascript import { init } from './init'; import { trackEvent } from './events'; // Initialize the tracker with configuration init({ domain: window.location.hostname, apiHost: 'https://plausible.io/api/event' }); // Example of tracking an event window.plausible = function() { trackEvent('pageview'); }; // Automatically track pageview on load window.plausible(); ``` -------------------------------- ### Plausible Analytics Technology Stack Source: https://github.com/plausible/analytics/blob/master/README.md Details the technologies used in Plausible Analytics. It's an Elixir/Phoenix application using PostgreSQL for general data and ClickHouse for stats. The frontend utilizes TailwindCSS for styling and React for interactivity. ```Elixir defmodule PlausibleAnalytics do # Plausible is a standard Elixir/Phoenix application # It uses PostgreSQL for general data and ClickHouse for stats. # Frontend uses TailwindCSS and React. end ``` -------------------------------- ### Run Tests (Bash) Source: https://github.com/plausible/analytics/blob/master/tracker/README.md Executes tests in UI mode using Playwright, which is helpful for debugging. ```bash npm run playwright --ui ``` -------------------------------- ### Initialize Plausible Analytics Source: https://github.com/plausible/analytics/blob/master/tracker/test/fixtures/engagement-manual.html Initializes the Plausible Analytics tracking object on the window. This function ensures that the `plausible` function is available and ready to accept tracking events. ```javascript window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) } ``` -------------------------------- ### Generate Fake Pageview Source: https://github.com/plausible/analytics/blob/master/CONTRIBUTING.md Generates a fake pageview event for a dummy site to test analytics tracking. ```Shell mix send_pageview ``` -------------------------------- ### Compile Tracker Code (JavaScript) Source: https://github.com/plausible/analytics/blob/master/tracker/README.md Compiles the tracker code using Node.js. Supports watching for changes and updating the web snippet. ```javascript node compile.js node compile.js --watch node compile.js --web-snippet ``` -------------------------------- ### Initialize Plausible Tracking Source: https://github.com/plausible/analytics/blob/master/tracker/test/fixtures/engagement-hash-manual.html Initializes the global `window.plausible` function, which is used to send pageview events and other analytics data. It ensures the function and its queue are available, even if the script hasn't loaded yet. ```javascript window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) } ``` -------------------------------- ### Navigate to Blog Post - JavaScript Source: https://github.com/plausible/analytics/blob/master/tracker/test/fixtures/engagement-hash-pageview-props.html Navigates to a specific blog post by setting an event author attribute, updating the window location hash, and displaying post content. It requires the author's name as input. ```javascript function goToBlogPost(author) { document.getElementById('plausible-script').setAttribute('event-author', author) window.location.hash = `/${author}-post` document.getElementById('content').innerHTML = `Hello, I'm ${author} and this is my post.` } ``` -------------------------------- ### Compile Variants Configuration Source: https://github.com/plausible/analytics/blob/master/tracker/ARCHITECTURE.md Defines the different compiled variants of the Plausible tracker script, including web snippets and npm packages, along with associated global compile flags. This file is crucial for managing the build process and ensuring all necessary variants are generated. ```json { "manualVariants": [ { "name": "tracker", "compiler": "tracker/compiler/index.js", "entry": "tracker/src/index.js", "output": "dist/script.js", "format": "esm", "minify": true, "env": { "NODE_ENV": "production", "PLASM_TRACKER_VERSION": "v1.0.0" } } ], "legacyVariants": [ { "name": "tracker-legacy-v1", "compiler": "tracker/compiler/index.js", "entry": "tracker/src/index.js", "output": "dist/script-legacy-v1.js", "format": "iife", "minify": true, "env": { "NODE_ENV": "production", "PLASM_TRACKER_VERSION": "v1.0.0", "PLASM_LEGACY_MODE": "v1" } } ] } ``` -------------------------------- ### Download Country Database Source: https://github.com/plausible/analytics/blob/master/CONTRIBUTING.md Fetches the geolocation database required for country-specific analytics. ```Shell mix download_country_database ``` -------------------------------- ### Stop Postgres Container Source: https://github.com/plausible/analytics/blob/master/CONTRIBUTING.md Stops and removes the PostgreSQL database container. ```Shell make postgres-stop ``` -------------------------------- ### Plausible Analytics Events API Source: https://github.com/plausible/analytics/blob/master/README.md This snippet demonstrates how to send events directly to the Plausible Analytics events API. It's useful for tracking custom conversions and user interactions on your website. Ensure you have your Plausible domain and API key configured. ```JavaScript fetch("https://plausible.io/api/event", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ name: "Custom Event Name", domain: "yourdomain.com", url: window.location.href, props: { // Optional custom properties plan: "Pro", level: 5 } }) }); ``` -------------------------------- ### Track Revenue Events with Plausible Source: https://github.com/plausible/analytics/blob/master/tracker/npm_package/README.md Tracks revenue-related events, such as purchases, with Plausible Analytics. This includes the amount and currency of the transaction. ```javascript import { track } from '@plausible-analytics/tracker' track('Purchase', { revenue: { amount: 15.99, currency: 'USD' } }) ``` -------------------------------- ### Stop Clickhouse Container Source: https://github.com/plausible/analytics/blob/master/CONTRIBUTING.md Stops and removes the ClickHouse database container. ```Shell make clickhouse-stop ``` -------------------------------- ### Elixir Tracker Plug Source: https://github.com/plausible/analytics/blob/master/tracker/ARCHITECTURE.md Elixir code responsible for serving the tracker script and managing site-specific tracker script configurations. This plug likely intercepts requests for the tracker script and interpolates necessary configuration data before serving it. ```elixir defmodule PlausibleWeb.Plugs.TrackerPlug do import Plug.Conn def init(opts), do: opts def call(conn, _opts) do case conn.request_path do "/js/script.js" -> conn |> put_resp_content_type("application/javascript") |> send_resp(200, Plausible.Tracker.script_content(conn.assigns.site_config)) _ -> conn end end end defmodule PlausibleWeb.Tracker do def script_content(site_config) do # Logic to interpolate site_config into the JS tracker script "console.log('Plausible tracker loaded with config:', \"#{inspect(site_config)}\");\n" end end ``` -------------------------------- ### Track Events with Callbacks in Plausible Source: https://github.com/plausible/analytics/blob/master/tracker/npm_package/README.md Tracks an event and provides a callback function to handle the result of the tracking request. This allows for custom logic based on whether the event was sent or ignored. ```javascript import { track } from '@plausible-analytics/tracker' track('some-event', { callback: (result) => { if (result?.status) { console.debug("Request to plausible done. Status:", result.status) } else if (result?.error) { console.log("Error handling request:", result.error) } else { console.log("Request was ignored") } } }) ``` -------------------------------- ### JavaScript Compilation Script Source: https://github.com/plausible/analytics/blob/master/tracker/ARCHITECTURE.md A script responsible for bundling and minifying different variants of the tracker script in parallel. It utilizes tools like Rollup and SWC to achieve efficient compilation and code optimization. ```javascript import { rollup } from 'rollup'; import swc from '@swc/core'; import json from '@rollup/plugin-json'; import resolve from '@rollup/plugin-node-resolve'; const variants = require('./compiler/variants.json'); async function compileVariant(variant) { const bundle = await rollup({ input: variant.entry, plugins: [ resolve(), json() ], // Add SWC plugin for minification and transpilation // This is a simplified representation, actual SWC integration might be more complex // For minification, SWC's minify option would be used during the build process }); const { output } = await bundle.generate({ format: variant.format || 'esm', file: variant.output, // SWC minification options would be configured here or via a .swcrc file // minify: true, // mangle: { reserved: ['plausible'] } }); // In a real scenario, you would write the output to the file system console.log(`Compiled ${variant.name} to ${variant.output}`); // await fs.writeFile(variant.output, output[0].code); } async function compileAll() { for (const variant of variants.manualVariants) { await compileVariant(variant); } for (const variant of variants.legacyVariants) { await compileVariant(variant); } } compileAll(); ``` -------------------------------- ### Uninstall Pre-commit Hooks Source: https://github.com/plausible/analytics/blob/master/CONTRIBUTING.md Removes pre-commit hooks from the development environment. ```Shell pre-commit uninstall ``` -------------------------------- ### Track Custom Events with Plausible Source: https://github.com/plausible/analytics/blob/master/tracker/npm_package/README.md Tracks custom events with Plausible Analytics, optionally including custom properties. This is used for specific user interactions beyond page views. ```javascript import { track } from '@plausible-analytics/tracker' track('signup', { props: { tier: "startup" } }) ``` -------------------------------- ### Go to Home Page - JavaScript Source: https://github.com/plausible/analytics/blob/master/tracker/test/fixtures/engagement-hash-pageview-props.html Resets the Plausible Analytics script by removing the event author attribute, clearing the window location hash, and clearing the displayed content. This function returns the user to the homepage. ```javascript function goHome() { document.getElementById('plausible-script').removeAttribute('event-author') window.location.hash = '' document.getElementById('content').innerHTML = '' } ``` -------------------------------- ### Trigger Plausible Pageview with Default URL Source: https://github.com/plausible/analytics/blob/master/tracker/test/fixtures/manual.html This snippet demonstrates how to trigger a Plausible pageview using the default URL (location.href). It's designed to be used within a Playwright test environment. ```javascript document.addEventListener('click', (e) => { if (e.target.id === 'pageview-trigger') { window.plausible('pageview') } }) ``` -------------------------------- ### Update Page Content and Track Views Source: https://github.com/plausible/analytics/blob/master/tracker/test/fixtures/engagement-hash-manual.html Updates the page title and sends a 'pageview' event to Plausible Analytics based on the current URL hash. It listens for hash changes to dynamically update the content and tracking. ```javascript const titleNode = document.getElementById('page-title') function updateContent() { if (location.hash === '#about') { titleNode.innerHTML = 'About us' window.plausible('pageview', {u: 'http://localhost:3000/#about-us'}) } else { titleNode.innerHTML = 'Home' window.plausible('pageview', {u: 'http://localhost:3000/#home'}) } } window.plausible('pageview', {u: 'http://localhost:3000/#home'}) window.addEventListener('hashchange', updateContent); ``` -------------------------------- ### Trigger Pageview with Custom URL Source: https://github.com/plausible/analytics/blob/master/tracker/test/fixtures/engagement-manual.html This snippet demonstrates how to trigger a Plausible Analytics pageview event with a custom URL. It listens for a click event on an element with the ID 'pageview-trigger-custom-url' and then calls `window.plausible` with the 'pageview' event and a custom URL. ```javascript document.addEventListener('click', (e) => { if (e.target.id === 'pageview-trigger-custom-url') { window.plausible('pageview', {u: 'https://example.com/custom/location'}) } }) ``` -------------------------------- ### JavaScript DOMContentLoaded and Scroll Event Handling Source: https://github.com/plausible/analytics/blob/master/tracker/test/fixtures/scroll-depth-content-onscroll.html This snippet demonstrates how to add an event listener for DOMContentLoaded to initialize functionality and a scroll event listener to detect when the user reaches the bottom of the page. Upon reaching the bottom, it dynamically creates and appends a new div element with specific styling and an ID. ```javascript document.addEventListener("DOMContentLoaded", () => { const mainDiv = document.getElementById("main"); let loadedMore = false; window.addEventListener("scroll", () => { if (!loadedMore && window.innerHeight + window.scrollY >= document.body.offsetHeight) { loadedMore = true; const newDiv = document.createElement("div"); newDiv.setAttribute("id", "more-content"); newDiv.setAttribute("style", "height: 2000px; background-color: lightcoral;"); document.body.appendChild(newDiv); } }); }); ``` -------------------------------- ### Trigger Pageview with Custom Properties Source: https://github.com/plausible/analytics/blob/master/tracker/test/fixtures/engagement-manual.html This snippet shows how to trigger a Plausible Analytics pageview event with custom properties. It listens for a click event on an element with the ID 'pageview-trigger-custom-props' and then calls `window.plausible` with the 'pageview' event and custom properties, such as an 'author'. ```javascript document.addEventListener('click', (e) => { if (e.target.id === 'pageview-trigger-custom-props') { window.plausible('pageview', {props: {author: 'John'}}) } }) ``` -------------------------------- ### Trigger Plausible Custom Event with Default URL Source: https://github.com/plausible/analytics/blob/master/tracker/test/fixtures/manual.html This snippet illustrates triggering a custom event in Plausible Analytics using the default URL. It allows for tracking specific user interactions on your site. ```javascript document.addEventListener('click', (e) => { if (e.target.id === 'custom-event-trigger') { window.plausible('CustomEvent') } }) ``` -------------------------------- ### Trigger Plausible Pageview with Custom URL Source: https://github.com/plausible/analytics/blob/master/tracker/test/fixtures/manual.html This snippet shows how to trigger a Plausible pageview with a specified custom URL. This is useful for testing analytics on different pages or specific routes within your application. ```javascript document.addEventListener('click', (e) => { if (e.target.id === 'pageview-trigger-custom-url') { window.plausible('pageview', {u: 'https://example.com/custom/location'}) } }) ``` -------------------------------- ### Track Non-Interactive Events with Plausible Source: https://github.com/plausible/analytics/blob/master/tracker/npm_package/README.md Tracks an event as non-interactive, which means it will not affect bounce rate calculations in Plausible Analytics. Useful for events like video plays. ```javascript track('autoplay', { interactive: false }) ``` -------------------------------- ### Trigger Plausible Custom Event with Custom URL Source: https://github.com/plausible/analytics/blob/master/tracker/test/fixtures/manual.html This snippet demonstrates triggering a custom event with a custom URL in Plausible Analytics. This is valuable for tracking events associated with specific pages or states. ```javascript document.addEventListener('click', (e) => { if (e.target.id === 'custom-event-trigger-custom-url') { window.plausible('CustomEvent', {u: 'https://example.com/custom/location'}) } }) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.