Hello @nikola,
Again, querying data models like navigation
, footer
, and utility bar
should not trigger tracking pixels as they are not considered visual content models.
When querying a page model that results in a 404 (i.e., the page does not exist), Builder.io tracking pixels can still be triggered. This is primarily due to how the SDK, alongside the server-side logic, handles the requests and responses.
To prevent the tracking pixels from firing when rendering a 404 page, you can try and implement the following steps:
- Implement a check to disable tracking specifically when rendering a 404 page:
import { builder } from '@builder.io/sdk';
// Replace with your Public API Key
builder.init(YOUR_API_KEY);
export async function getPageContent(urlPath) {
const content = await builder.get('page', {
userAttributes: {
urlPath: urlPath,
},
prerender: false,
}).toPromise();
if (!content) {
builder.canTrack = false; // Disable tracking for 404 pages
return null;
}
return content;
}
- Modify Fetch Function : Use a custom function to fetch the builder content while ensuring tracking is disabled if the content does not exist:
async function fetchWithConditionalTracking(model, options) {
builder.canTrack = false; // Disable tracking
const data = await builder.get(model, options);
if (data) {
builder.canTrack = true; // Enable tracking if data exists
}
return data;
}
// Example usage in getStaticProps or similar function
export async function getStaticProps({ params }) {
const pageContent = await fetchWithConditionalTracking('page', {
userAttributes: {
urlPath: '/' + (params?.page?.join('/') || ''),
},
prerender: false,
});
return {
props: {
page: pageContent ? pageContent : null,
},
revalidate: 5,
};
}
- Handling in Layout and Query Changes : Ensure that any dynamic layout or query changes also respect this tracking behavior:
// Example API call handling with conditional tracking
builder.getAll('content-page', { ...options, prerender: false }).then(results => {
if (!results.length) {
builder.canTrack = false; // Disable tracking if no results found
}
// Handle results
});
Let us know how the above suggestions work for you!
Thanks,