AUTOMATE COMMERCE
Requirements
- You must have docker desktop, or docker and docker-compose
- Node Js and NPM
Getting Started
- create a .env.development file
- Run
docker-compose up
from the root of the project to create a local postgres database and object storage.
- Open a new terminal window and Run
npm install && npx prisma generate
to install dependencies and generate the prisma client
- Run
npx prisma db push && npx prisma db seed
to create tables and seed the development db
- Run
npm run dev
to run the development server.
Seeding the database
- to seed the database you can run
npx prisma db seed
. this command will clear and re-seed the DB
- after running the seed command you must clear http cookies as well as local storage to remove any old leads / carts
- clear http cookies by navigating or making a request to the following url
http://localhost:3000/api/cookies/clear
- clear the local sotrage by opening up your browser console and running
localStorage.clear()
Searching
make sure you have the pg_trgm extension enabled in your postgres database. To do this you can run the following:
- to enter psql shell
psql -h localhost -p 5432 -U postgres -d ergonomica
- search for the pg_trgm extension
SELECT * FROM pg_extension WHERE extname = 'pg_trgm';
this should display atleast 1 row
- if no extension is found run
CREATE EXTENSION IF NOT EXISTS pg_trgm;
- run
exit
to exit psql shell
Testing
End to end tests are held in the cypress/e2e/
directory.
You can run the test suite without having to install any depencies manually by running ./test.sh
.
If you want to run tests manually you can run npx cypress open
while having another terminal running the dev server. Beware that performance tests will fail when running the test suite with the dev server; If you want to test performance run npm run build && npm run start
to build and serve the app.
Analytics
to use analytics you must set the following env variables. The analytics package has modules for the following analytics providers:
- Posthog (opensource analytics)
- Meta (pixel & conversion API)
- Google Analytics
- X analytics
The analytics package is built in a way where the main methods exported will trigger events in all of the selected providers; for example if google analytics, posthog and meta are selected; a pageview event will trigger a pagheview in all of these platfoirms simultaneously. This makes it very easy to add new providers without having to go to different places in the code.
To get everything working make sure you follow the steps bellow and setup the follwoing environment variables:
# toggle usage of analytics
NEXT_PUBLIC_USE_ANALYTICS
# post hog
NEXT_PUBLIC_POSTHOG_HOST
NEXT_PUBLIC_POSTHOG_KEY
# google analytics
NEXT_PUBLIC_GA_TRACKING_ID
# metap pixel
NEXT_PUBLIC_FB_PIXEL_ID
# twitter
NEXT_PUBLIC_TWITTER_BEARER_TOKEN
To test events in google analytics and meta while in development you must be using some kind of ip tunnel like ngrok
Setting up posthog
- create a free account in posthog
- go to your project settings
- copy your project API Key and set an environment variable
NEXT_PUBLIC_POSTHOG_KEY=<YOUR_PROJECT API_KEY>
- set an environment variable
NEXT_PUBLIC_POSTHOG_HOST=https://app.posthog.com
- View realtime events in posthog
Setting up Googl Analytics (GA4)
- Follow these instructions to setup google analytics 4 (GA4)
- Create a Property
- Add a datastream to the property
- In your property setting click on Data collection and modification > Data Streams
- click on your datastream and edit the Stream details, add your ngrok URL as the website URL and call your data stream development
- copy your measurement ID and set the environment vairable
NEXT_PUBLIC_GA_TRACKING_ID=<YOUR_MEASUREMENT_ID>
- To view realtime test analytics, go to https://analytics.google.com/analytics/web/?authuser=1#/p/realtime
Setting up Meta
Pixel
- create a new pixel
- add the id of the new meta pixel in your env viariable
- Now you can monitor your pixels activity by accesing this URL: https://business.facebook.com/events_manager2/list/dataset/
- All the activities carried out in the website should register in this page in the ovberviw. beware there is a 30 minute lag for events to register
Conversion API
- After creating your pixel, follow instructions here to setup the converiosn API
- go to the Graph API Explorer
click on generate Access Token and add some permissions
- we will add the email and page_events permissions.
- click on generate token
- add this token in your environment variable
FB_ACCESS_TOKEN
- lets make a GET request to
/me?fields=id,name
- should display user info
- now in the events manager page, go to datasets, select the dataset corresponding to your development pixel, click on the settings tab and scroll down to Sharing, then click on Business Manager Settings
- On the people tab, click on the user associated to the conversion API, usually Conversion API System User.
- When the tab opens up, click on the square icon on the uper right side of the tab to open it in Users view. click on the dataset and click on assign assets.
- select datasets, check your pixels dataset and tick full control
- now go back to the fb events manager
- click on the Test Events tab
- click on
Confirm your server’s events are set up correctly
- copy the test event code and set up the enviornment vairable
FB_TEST_CODE=<YOUR_TEST_EVENT_CODE>
- click on the Graph API Explorer button
- Submit the request. you sould get a success response like below
{
"events_received": 1,
"messages": [
],
"fbtrace_id": "A5dI0gET-Do7zoc6hPQ_iHr"
}
Test Events
- To test meta events in realtime you must be using some kind of ip tunnel like ngrok
- go to the meta events manager test events page: https://business.facebook.com/events_manager2/list/dataset//test_events
- click on
Confirm your websites events are set up correctly
- enter the URL of your ngrok ip tunnel (in your ngrok terminal window the adress next to Forwarding)
- Click on open URL and make some events on the page i.e. visit a product page and the product to cart
- initially it will take a few minutes but you should start seeing the events log in the test events page