Use JavaScript to limit Google Analytics and Yandex.Metrica to track only the referral sources and screen sizes

I prefer not to include external JavaScript on my website for unnecessary tracking purposes.

However, I do need to gather referrer and screen size information, which cannot be achieved with a regular HTML img tag alone.

What is the most standard-compliant, browser-supported, and visually-friendly method to load an invisible 1x1 pixel gif image with referrer and screen size parameters in the URL?

I am seeking a general solution, but feel free to reference the Google Analytics GIF Request Parameters if needed.

Answer №1

Retrieve the referrer using the code document.referrer.

Access the screen resolution with window.screen.width and window.screen.height.

To trigger the tracking pixel request in the browser, utilize new Image() and remember to sanitize the values by using encodeURIComponent before appending them to the URL.

var r = encodeURIComponent(document.referrer),
    x = window.screen.width,
    y = window.screen.height,
    img = new Image();
img.src = '/tracking.gif?x=' + x + '&y=' + y + '&r=' + r;

Keep in mind that subsequent requests will be cached, potentially affecting pageview count. However, this can be beneficial for implicit unique visitor tracking. To prevent caching, consider adding the current timestamp using new Date().getTime(); as an extra parameter.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The websocket connection established with apollo-server is somehow producing nonsensical output for the connection params

onConnect should receive the connectionParams supplied by the client and then validate that the token has not expired by checking the token property on the connectionParams object. On the client-side, I send these parameters in the following manner: const ...

Unusual behavior involving the selection of $stateParams

Seeking a solution for updating angular-ui route parameters based on select field changes. Issue: The route successfully updates with the selected parameter, but the select field does not reflect the change in option selection. Check out the Plunkr. Clic ...

AngularJS: ng-repeat excluding duplicate keys

Here is a collection of objects I am working with: [{ key:test1 name: name1 }, { key:test1 name: name2 }, { key:test2 name: name3 }] I am currently using ng-repeat to showcase them: <tr ng-repeat=item in list> <td>{{item.key}}</td ...

The width of a CSS border determines its height, not its width

When I try to use border-width: 100px; to set the horizontal width to 100px, it ends up setting the height to 100px instead. Any help would be greatly appreciated. .border-top-green { border-top: 1px solid #4C9962; border-width: 100px; padding-t ...

Can Ember store in Route handle Post requests?

Is there a way to use ember store functionality similar to this.store.findAll('report') for making POST requests with postObj in my route? Also, how should I handle the response received from these requests? Right now, I am sending ajax POST requ ...

Chrome reports a Javascript error: indicating that it is not recognizing the function

When working with a js file and html, I encountered an issue where one function works fine but another prompts an error in Chrome: Uncaught TypeError: specification_existing is not a function I'm puzzled as to why one function works while the othe ...

Integrate Vue.js project to store images in MS Azure cloud storage

I am currently exploring the VueJs framework and looking to integrate my project with Azure. However, I am unsure of the process. Previously, I worked with Firebase - is it a similar technique where we configure storage within the project? Recently, I cre ...

The viewport scaling issue occurs when transitioning from landscape to portrait orientation

While developing a mobile version of my website, I have encountered an issue with the behavior when rotating my iPhone device. Everything looks fine in landscape orientation, but upon rotation back to portrait, the viewport remains stuck at the landscape s ...

Using NodeJS to handle server side FormData

Currently, in the process of building a web application with nodejs on the server-side, I am facing a challenge of transferring PDF files from the client to the server. Client side: var files = new FormData(); var count = 0; $('#tableSlideId tr&apos ...

Extracting Tik Tok videos from their URLs using PHP web scraping

I'm attempting to save videos from the following URL: Original: https://api.tiktokv.com/aweme/v1/playwm/?video_id=v09044340000bq0vmd39if221ld9o5n0 The link then changes to this: http://v16m.tiktokcdn.com/e50056249a925719f71ce8618053e173/5eee4e39/vid ...

Show a dynamic Swiper carousel upon selecting a thumbnail in an image gallery

I am in the process of creating a thumbnail gallery that includes a slider feature using Swiper. The default setup has the slider hidden, and once an image is clicked, the slider should appear with the selected image. To close the slider and return to the ...

What is the best method for submitting information using AJAX and jQuery?

Here is the HTML code in question: <script src='https://code.jquery.com/jquery-1.12.4.min.js'></script> <p>1</p> <!-- this content gets loaded with <?php echo $item->id; ?> --> <a id='delBtn1' ...

AngularJs - Show the response only upon verifying the correct answer

Let me provide an overview of what has been implemented so far: When a user selects an answer in radio buttons and clicks on "Check Answer", the system displays either "Correct" (in green) or "Incorrect" (in red) in the first answer field. Additionally, th ...

Executing a search and replace function using a delay within a foreach loop - the ultimate guide

Below is a snippet of code where I attempt to perform find and replace within an array by searching for keys and replacing them with corresponding values. However, the expected functionality does not work as intended, leading to multiple searches for &apos ...

Having trouble with opening and closing popup windows in JavaScript while using Android frames?

To display additional information for the user, I create a new tab using the following code: window.open("/Home/Agreement", "_blank"); Within the Agreement View, there is a button with JavaScript that allows the user to close the Popup and return to the m ...

Using javascript to eliminate a block of HTML code

In my AngularJS project, I am using owl-carousel and attempting to reinitialize the carousel after a change in data by using $(element).data('owlCarousel').destroy(); while also removing this block: <div class="owl-wrapper"> <div class= ...

Count characters in multiple text inputs with AngularJS

Currently, I am developing an Angular JS application with the help of Angular-UI (bootstrap). In this app, there are multiple input boxes where users can enter data, which is then displayed in a div. My goal is to have a character count that applies to al ...

Unable to access the property 'function' of an undefined value

I've been attempting to call a function from another function within my React application. However, I am encountering an error that reads: Error in login TypeError: Cannot read property 'loadDashboard' of undefined. Despite researching simil ...

Tips for persisting objects created in PHP while utilizing XMLHttpRequest

Currently, I am working on a web page with the index.php file structured as shown below: include('UserClass.php'); include('html/top.php'); $user = new User(); if (isset($_POST['user'], $_POST['pass'])) { $user-& ...

Understanding Arrays in Angular JSIn this article, we

I am new to working with Angular JS. Currently, I am populating an array and attempting to showcase its contents on the HTML page using ng-repeat. $scope.groupedMedia = []; // Elements are being added through a for loop. $scope.groupedMedia[year].push(r ...