Last active
March 22, 2022 14:57
-
-
Save simonrolph/687a358ac770f1959740468e9f2f20ef to your computer and use it in GitHub Desktop.
A 1x1 pixel tracker and logger minimal example using plumber API in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This is a plumber api for setting up a basic pixel tracker | |
# here's a random page I found about what pixel trackers are: https://en.ryte.com/wiki/Tracking_Pixel | |
# When the pixel is loaded by an embedded image eg. | |
# | |
# <img src="baseurl.com/pixel?log=this_is_the_message_I_want_to_log" > | |
# | |
# It creates a new folder for the date if needed then writes a log file into the folder | |
library(plumber) | |
library(png) | |
library(fs) | |
library(logger) | |
#* @apiTitle Pixel Tracker and Logger | |
#* Return a pixel | |
#* @param log Some identifier to be logged | |
#* @get /pixel | |
#* @serializer contentType list(type='image/png') | |
function(log=""){ | |
#generate a pixel | |
pixel <- writePNG(matrix(1)) | |
#create a folder for the day | |
log_dir <- paste0("logs/",Sys.Date()) | |
if (!fs::dir_exists(log_dir)) fs::dir_create(log_dir) | |
log_appender(appender_tee(tempfile("pixel_tracker_", log_dir, ".log"))) | |
log_info(log) | |
# return the pixel | |
pixel | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment