Add google analytics to your Next.js app ✨

Wassup everyone, let’s see how to add google analytics to your Next.js site. Google Analytics will help you to see the performance of each page, how well the site is doing in countries, different devices, etc.



Setup Google Analytics

Go to Google Analytics and click on admin in the sidebar.

image.png

After clicking on admin you will see a screen similar to this-

image.png

Click on “Create Property”, now fill in the form with your details.

image.png

You also need to give basic business details.

After you click submit your property will be created! You will see this screen and as we are using Next.js, select web-

image.png

Fill in your website URL and title-

image.png

Click on add on Add new on-page inside Tagging instructions

image.png

Copy the ID that you see and copy it as we are going to need it!



Using Google Analytics in Next.js app

Go inside pages/_app.tsx and these two Next.js Scripts-

  <Script
    strategy="lazyOnload"
    src=`https://www.googletagmanager.com/gtag/js?id=$process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS`
  />

  <Script id="google-analytics" strategy="lazyOnload">
     [];
            function gtag()dataLayer.push(arguments);
            gtag('js', new Date());
            gtag('config', '$process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS', 
              page_path: window.location.pathname,
            );
                `
  </Script>

This is going to load google analytics via this script. As you can see we are using env variables for the google-analytics tag because it must remain a secret 🤫, so if you don’t already have a .env.local file create one and add the following-

NEXT_PUBLIC_GOOGLE_ANALYTICS=YOUR_GOOGLE_ANALYTICS_ID

Paste in the ID that we copied in the last step! Since we are changing the env variables you need to restart the server too.

As we are using the Next.js script we also need to import it-

import Script from "next/script";

Open your localhost app in one tab and google analytics in another. If you click on Real-time inside of Reports you will see that you have 1 user!

image.png



Conclusion

Adding google analytics to your Next.js app was this easy! Hope you found it useful and insightful. See ya next time ✌️



Useful links

Google Analytics

Next.js

Connect with me


Source link

Foobar: Please Pass the Coded Messages

You need to pass a message to the bunny workers, but to avoid detection, the code you agreed to use is… obscure, to say the least. The bunnies are given food on standard-issue plates that are stamped with the numbers 0-9 for easier sorting, and you need to combine sets of plates to create the numbers in the code. The signal that a number is part of the code is that it is divisible by 3. You can do smaller numbers like 15 and 45 easily, but bigger numbers like 144 and 414 are a little trickier. Write a program to help yourself quickly create large numbers for use in the code, given a limited number of plates to work with.

You have L, a list containing some digits (0 to 9). Write a function solution(L) which finds the largest number that can be made from some or all of these digits and is divisible by 3. If it is not possible to make such a number, return 0 as the solution. L will contain anywhere from 1 to 9 digits. The same digit may appear multiple times in the list, but each element in the list may only be used once.

To provide a Java solution, edit Solution.java
To provide a Python solution, edit solution.py

Your code should pass the following test cases.
Note that it may also be run against hidden test cases not shown here.

Input:
solution.solution([3, 1, 4, 1])
Output:
4311

Input:
solution.solution([3, 1, 4, 1, 5, 9])
Output:
94311

Solution:
python

def to_int(l):
    if not l:
        return 0
    return int("".join([str(d) for d in l]))

def solution(l):
    l = sorted(l, reverse=True)
    n = len(l)
    s = sum(l)
    if s % 3 == 0:
        # already divisible by 3 => use all digits
        return to_int(l)
    elif s % 3 == 1:
        # check backward if there is a digit that is 1 mod 3
        i = n - 1
        while i >= 0:
            if l[i] % 3 == 1:
                return to_int(l[:i] + l[i+1:])
            i -= 1
        # there must be two digits that each of them is 2 mod 3
        i = n - 1
        while i >= 0:
            if l[i] % 3 == 2:
                break
            i -= 1
        j = i - 1
        while j >= 0:
            if l[j] % 3 == 2:
                break
            j -= 1
        return to_int(l[:j] + l[j+1:i] + l[i+1:])
    else:
        # check backward if there is a digit that is 2 mod 3
        i = n - 1
        while i >= 0:
            if l[i] % 3 == 2:
                return to_int(l[:i] + l[i+1:])
            i -= 1
        # there must be two digits that each of them is 1 mod 3
        i = n - 1
        while i >= 0:
            if l[i] % 3 == 1:
                break
            i -= 1
        j = i - 1
        while j >= 0:
            if l[j] % 3 == 1:
                break
            j -= 1
        return to_int(l[:j] + l[j+1:i] + l[i+1:])

Source link