### Get site details Source: https://plausible.io/docs/sites-api Example using curl to retrieve details of a specific site. ```bash curl -X GET https://plausible.io/api/v1/sites/test-domain.com \ -H "Authorization: Bearer ${TOKEN}" ``` -------------------------------- ### Get Goals for a Site Source: https://plausible.io/docs/sites-api Example of using the GET /api/v1/sites/goals endpoint to retrieve a list of existing goals for a given site. ```bash curl -X GET https://plausible.io/api/v1/sites/goals?site_id=test-domain.com \ -H "Authorization: Bearer ${TOKEN}" ``` -------------------------------- ### Pagination Example Source: https://plausible.io/docs/sites-api Example of a list response with pagination metadata. ```json { "sites": [...], "meta": { "after": "Z2xc...", "before": null, "limit": 100 } } ``` -------------------------------- ### Get Site Guests Source: https://plausible.io/docs/sites-api Example of retrieving a list of guests for a site using the GET /api/v1/sites/guests endpoint. ```bash curl -X GET https://plausible.io/api/v1/sites/guests?site_id=test-domain.com \ -H "Authorization: Bearer ${TOKEN}" ``` -------------------------------- ### Get Custom Properties for a Site Source: https://plausible.io/docs/sites-api Example of using the GET /api/v1/sites/custom-props endpoint to retrieve a list of existing custom properties for a site. ```bash curl -X GET https://plausible.io/api/v1/sites/custom-props?site_id=test-domain.com \ -H "Authorization: Bearer ${TOKEN}" ``` -------------------------------- ### Order By Example Source: https://plausible.io/docs/stats-api An example of how to specify custom ordering for query results using a list of tuples. ```json [["visitors", "desc"], ["visit:country_name", "asc"]] ``` -------------------------------- ### NPM package tracker update example Source: https://plausible.io/docs/change-domain-name Example of how to update the init options for the @plausible-analytics/tracker library when changing the domain. ```javascript init({ domain: "your_new_domain.com", ... }) ``` -------------------------------- ### Imported Visitors CSV Example Source: https://plausible.io/docs/csv-import Example of the header and first few lines of a CSV file containing imported visitor data. ```bash $ head imported_visitors_20230209_20240123.csv "date","visitors","pageviews","bounces","visits","visit_duration" "2023-02-09",119,304,77,134,21070 "2023-02-10",166,325,117,180,21467 "2023-02-11",117,233,92,128,16506 "2023-02-12",221,356,187,237,13673 "2023-02-13",1449,2681,1306,1722,207955 "2023-02-14",1724,3075,1440,1962,211402 "2023-02-15",4226,13653,3559,5915,1043890 "2023-02-16",5793,18555,4796,7969,1496917 "2023-02-17",4941,16870,4098,6987,1444726 ``` -------------------------------- ### Create a site Source: https://plausible.io/docs/sites-api Example using curl to create a new site with specified domain, timezone, and tracker script configuration. ```bash curl -X POST https://plausible.io/api/v1/sites \ -H "Authorization: Bearer ${TOKEN}" \ -H "Content-Type: application/json" \ -d '{ "domain": "test-domain.com", "timezone": "Europe/London", "tracker_script_configuration": { "file_downloads": true, "form_submissions": true, "outbound_links": true } }' ``` -------------------------------- ### Breakdown API Endpoint Example Source: https://plausible.io/docs/stats-api-v1 Example of how to use the GET /api/v1/stats/breakdown endpoint to get visitors and bounce rate broken down by visit source. ```bash curl "https://plausible.io/api/v1/stats/breakdown?site_id=$SITE_ID&period=6mo&property=visit:source&metrics=visitors,bounce_rate&limit=5" \ -H "Authorization: Bearer ${TOKEN}" ``` -------------------------------- ### Create or Find Goal Source: https://plausible.io/docs/sites-api Example of using the PUT /api/v1/sites/goals endpoint to create or find a goal for a site. ```bash curl -X PUT https://plausible.io/api/v1/sites/goals \ -H "Authorization: Bearer ${TOKEN}" \ -H "Content-Type: application/json" \ -d '{ "site_id": "test-domain.com", "goal_type": "event", "event_name": "Signup" }' ``` -------------------------------- ### Paginate with After Parameter Source: https://plausible.io/docs/sites-api Example of retrieving the next page of results using the 'after' query parameter. ```bash curl -X GET https://plausible.io/api/v1/sites?after=AFTER_VALUE_FROM_LAST_RESPONSE \ -H "Authorization: Bearer ${TOKEN}" ``` -------------------------------- ### Realtime Visitors API Request Source: https://plausible.io/docs/stats-api-v1 Example cURL request to get the number of current visitors on a site. ```bash curl "https://plausible.io/api/v1/stats/realtime/visitors?site_id=$SITE_ID" \ -H "Authorization: Bearer ${TOKEN}" ``` -------------------------------- ### Combine data for multiple pages based on canonical URL Source: https://plausible.io/docs/custom-locations This example demonstrates how to use the `transformRequest` function to get the canonical URL from a meta tag and override the request URL, effectively combining pageviews for pages with the same canonical URL. ```javascript function transformRequest(payload) { // Get the canonical URL element var canonicalMeta = document.querySelector('link[rel="canonical"]'); // Use the canonical URL if it exists, falling back on the regular URL when it doesn't. if (canonicalMeta) { payload.u = canonicalMeta.href + window.location.search } return payload } // At the end, update plausible.init call plausible.init({ transformRequest: transformRequest }) ``` -------------------------------- ### List existing sites Source: https://plausible.io/docs/sites-api Gets a list of existing sites your Plausible account can access. ```bash curl -X GET https://plausible.io/api/v1/sites \ -H "Authorization: Bearer ${TOKEN}" ``` -------------------------------- ### Example of custom URL preparation Source: https://plausible.io/docs/custom-query-params Example of how to use the `prepareUrl` function with specific query parameters like 'article' and 'page' for a URL like `yoursite.com/blog/index.php?article=some_article&page=11`. ```javascript plausible('pageview', { url: prepareUrl(["article", "page"]) + window.location.search }); ``` -------------------------------- ### UTM Parameters Example Source: https://plausible.io/docs/metrics-definitions Example of a link with UTM parameters to track traffic sources. ```html https://yoursite.com/blog/post?utm_source=newsletter&utm_medium=email&utm_campaign=april-launch ``` -------------------------------- ### List existing sites response Source: https://plausible.io/docs/sites-api Example response for listing existing sites. ```json { "sites": [ { "domain": "test-domain1.com", "timezone": "Europe/London", }, { "domain": "test-domain2.com", "timezone": "Europe/London", }, { "domain": "test-domain3.com", "timezone": "Europe/London", } ], "meta": { "after": null, "before": null, "limit": 100 } } ``` -------------------------------- ### Example with custom properties Source: https://plausible.io/docs/events-api Example of a request body with custom properties. ```json {"name":"pageview","url":"http://dummy.site","domain":"dummy.site","props":{"author":"John Doe","logged_in":"false"}} ``` -------------------------------- ### Setup block [Required] Source: https://plausible.io/docs/shopify-integration This snippet is required for everything else that follows, but doesn't track anything by itself. Make sure to replace `yourdomain.com` on the first line with the name of your site in Plausible. Note that this snippet should be placed at the top of the code block. ```javascript const DATA_DOMAIN = 'yourdomain.com'; const script = document.createElement('script'); script.defer = true; script.dataset.domain = DATA_DOMAIN; script.dataset.api = 'https://plausible.io/api/event'; script.src = 'https://plausible.io/js/script.manual.revenue.js'; document.head.appendChild(script); window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) } ``` -------------------------------- ### Example curl request Source: https://plausible.io/docs/stats-api Example curl request to the Plausible Stats API. ```curl curl \ --request POST \ --header 'Authorization: Bearer YOUR-KEY' \ --header 'Content-Type: application/json' \ --url 'https://plausible.io/api/v2/query' \ --data '{ "site_id": "dummy.site", "metrics": ["visitors"], "date_range": "7d" }' ```