Day 33 – First Time App Is Run For The Day

Sometimes you need to know the first time each individual user runs the app for the day. Sometimes you need to know the first time a user runs the app for the day. Well… here is one way to do both.

ruleset a60x541 {
  meta {
    name "new-day-test"
    description <<
      for when you want to run a rule only once on a new day.
      ie. a (script like) rule to clean up or prep for new day.
    >>
    author "Mike Grace"
    logging on
  }

  rule check_the_day_for_a_single_user {
    select when pageview ".*"
    pre {
      today = time:strftime(time:now({"tz":"America/Denver"}), "%d");
      savedDay = ent:savedDay || 0;
    }
    if (today != savedDay) then {
      noop();
    }
    fired {
      raise explicit event new_day_for_user;
      set ent:savedDay today;
    } else {
      raise explicit event same_day_for_user;
    }
  }

  rule its_a_new_day {
    select when explicit new_day_for_user
    {
      notify("Hey!","It's a new day!") with sticky = true;
    }
  }

  rule same_day {
    select when explicit same_day_for_user
    {
      notify("Hello again","Looks like it is still the same day") with sticky = true;
    }
  }

  rule check_the_day_for_app {
    select when pageview ".*"
    pre {
      today = time:strftime(time:now({"tz":"America/Denver"}), "%d");
      savedDay = app:savedDay || 0;
    }
    if (today != savedDay) then {
      noop();
    }
    fired {
      raise explicit event new_day_for_app;
      set app:savedDay today;
    } else {
      raise explicit event same_day_for_app;
    }
  }

  rule its_a_new_day_for_app {
    select when explicit new_day_for_app
    {
      notify("App: Hey!","It's a new day!") with sticky = true;
    }
  }

  rule same_day_for_app {
    select when explicit same_day_for_app
    {
      notify("App: Hello again","Looks like it is still the same day") with sticky = true;
    }
  }

}
  • 12 checking to see if it is a new day for each individual user
  • 15 get current day
  • 16 get the saved day from last time it was run or set to ‘0’ if not yet set
  • 18 executed action block if it’s a new day
  • 22 raise event for new day rules
  • 23 save todays date as the saved day so we know while it is still the same day
  • 25 raise event for same day rules if it is still the same day
  • 43-58 do the same thing but only raise the event for the first user that runs it on the new day using app variables instead of entity variables

App run on example.com using bookmarklet for the first time

App run on example.com using bookmarklet for the second time within seconds of running it the first time

App run on example.com using bookmarklet for the first time in a different browser

Get the bookmarklet to try it out yourself!

Gratuitous day 33 Grace face

About these ads
This entry was posted in Kynetx. Bookmark the permalink.

One Response to Day 33 – First Time App Is Run For The Day

  1. Randall Bohn says:

    This is cool. You could give some kind of reward to the first person to run your app each day.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s