Removing Google Analytics and Replacing it with a cookie free Javascript Geo Tracking app

Running Google Analytics slows down site performance because adding any script to our html head tag adds overhead to our app. This includes any third party libraries also added to the head tag.

I found a free jsonp geo tracking app, pulled country, ci…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Rick Delpo

Running Google Analytics slows down site performance because adding any script to our html head tag adds overhead to our app. This includes any third party libraries also added to the head tag.

I found a free jsonp geo tracking app, pulled country, city and ip variables into my javascript, then passed these vars into a Lambda function and saved them as a json file to a simple AWS S3 bucket. This way I could avoid adding the google analytics to my head tag, thereby speeding up my performance a bit. I included the following code just before my end body tag.

<script type="text/javascript" src="https://ipinfo.io/json?callback=myCallback">

</script>

then my callback function looks like this

 function myCallback(post){
session = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15); //added 11-11 default session 

pg_name="whatever ur page name is";                  
country = post.country;
city =post.city;
ip_address = post.ip;

passVars(); //first run start script then run passvars javascript function to  pass vars to Lambda


        }  //end myCallback

my passvars javascript function looks like this..note we fetch Lambda inside this function

function passVars() {

var date = new Date().toLocaleDateString('en-US');
var now = new Date().toLocaleTimeString();
var out = new Date().toLocaleTimeString();

   //then pass 5 objects... ses, hit2 etc to lambda and call event.ses inside lambda..note, this object must be called article
const article = { ses: session, city2: city, hit2: date, date2:pg_name, ip2:ip_address,country2:country, time2:now, time3:out };
    //call Lambda function here, this next line is the AWS api gateway call to the Lambda function
fetch("https://xxxx.execute-api.us-east-2.amazonaws.com/default/saveS3", {

    method: "POST",
    // Adding body or contents to send
    body: JSON.stringify(article),
    // Adding headers to the request
    headers: {
        "Content-type": "application/json; charset=UTF-8"
    }
})

}

my Lambda function looks like this

const AWS = require('aws-sdk');
const fetch = require('node-fetch');
const s3 = new AWS.S3();

exports.handler = async (event) => {
       //first fetch ur current json file from s3 bucket
  const res = await fetch('https://xxxx.s3.us-east-2.amazonaws.com/tracker2.json');
  const json = await res.json();

  // then add a new record
        json.push({
          // get geo data, then pass vars into here from javascript file
            country: event.country2,
            session: event.ses,
            page_name: event.date2,
            hit: event.hit2,
            ip: event.ip2,
            time_in: event.time2,
            time_out: event.time3,
            event_name: event.city2

        });


   //then re write whole updated file back to s3 and it will overwrite existing file

  var params = {
      Bucket: 'xxxx',
         Key: 'tracker2.json',
         Body: JSON.stringify(json), //pass fetch result into body with update from push
         ContentType: 'json',
   };

   var s3Response = await s3.upload(params).promise();

};

My AWS S3 json file looks like this

[{"country":"Philippines","session":"j4w9nq38x4nl8v1fxy3ja","page_name":"Learn Java","hit":"11/22/2021","ip":"175.176.55.14","time_in":"7:52:35 PM","time_out":"7:52:35 PM"},{"country":"United States","session":"n0ja7c6z2vlec6tn7bd","page_name":"Learn Java","hit":"11/22/2021","ip":"66.249.66.130","time_in":"7:04:57 AM","time_out":"7:04:57 AM"}]

After passing variables to my S3 json file I can then render the result to html.

see the SQL section of my website at
https://javasqlweb.org
for more

Happy coding folks!


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Rick Delpo


Print Share Comment Cite Upload Translate Updates
APA

Rick Delpo | Sciencx (2023-01-08T18:06:50+00:00) Removing Google Analytics and Replacing it with a cookie free Javascript Geo Tracking app. Retrieved from https://www.scien.cx/2023/01/08/removing-google-analytics-and-replacing-it-with-a-cookie-free-javascript-geo-tracking-app/

MLA
" » Removing Google Analytics and Replacing it with a cookie free Javascript Geo Tracking app." Rick Delpo | Sciencx - Sunday January 8, 2023, https://www.scien.cx/2023/01/08/removing-google-analytics-and-replacing-it-with-a-cookie-free-javascript-geo-tracking-app/
HARVARD
Rick Delpo | Sciencx Sunday January 8, 2023 » Removing Google Analytics and Replacing it with a cookie free Javascript Geo Tracking app., viewed ,<https://www.scien.cx/2023/01/08/removing-google-analytics-and-replacing-it-with-a-cookie-free-javascript-geo-tracking-app/>
VANCOUVER
Rick Delpo | Sciencx - » Removing Google Analytics and Replacing it with a cookie free Javascript Geo Tracking app. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/01/08/removing-google-analytics-and-replacing-it-with-a-cookie-free-javascript-geo-tracking-app/
CHICAGO
" » Removing Google Analytics and Replacing it with a cookie free Javascript Geo Tracking app." Rick Delpo | Sciencx - Accessed . https://www.scien.cx/2023/01/08/removing-google-analytics-and-replacing-it-with-a-cookie-free-javascript-geo-tracking-app/
IEEE
" » Removing Google Analytics and Replacing it with a cookie free Javascript Geo Tracking app." Rick Delpo | Sciencx [Online]. Available: https://www.scien.cx/2023/01/08/removing-google-analytics-and-replacing-it-with-a-cookie-free-javascript-geo-tracking-app/. [Accessed: ]
rf:citation
» Removing Google Analytics and Replacing it with a cookie free Javascript Geo Tracking app | Rick Delpo | Sciencx | https://www.scien.cx/2023/01/08/removing-google-analytics-and-replacing-it-with-a-cookie-free-javascript-geo-tracking-app/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.