When should one count Sefiras HaOmer: before or after Aleinu? The Vilna Gaon (in Sha’arei Rachamim) says one should say Aleinu first while the Chok Yaakov (489:20), Pri Migadim (Aishel Avraham 489:15), and Mishnah Berurah (489:2) say the very opposite.
In order to better understand the basis for this machlokes, we must first discuss the origin of saying Aleinu. The Bach (Orach Chayim 133) explains that Aleinu was added to the conclusion of davening – right before people leave shul – so as to instill faith in the Oneness of Hashem’s kingship and strengthen orr emunah that He will one day remove all the detestable idolatry on earth. It helps us protect ourselves from being tempted to follow the beliefs and lifestyles of the nations among whom we dwell and do business with.
The Sha’arei Teshuvah (Orach Chayim 233:1), citing the Arizal, says that Aleinu is simply the conclusion of each of the three tefillos. The Arizal seems to maintain that Aleinu was instituted as part of davening.
The Chasam Sofer was accustomed not to say Aleinu after Minchah on erev Shabbos since we remain in shul for Ma’ariv. This custom accords with the explanation of the Bach who says Aleinu was instituted as the last tefillah one says prior to leaving shul. Since we don’t leave shul after Minchah on erev Shabbos, there’s no reason to say Aleinu. That’s also why we don’t say Aleinu after Musaf or Minchah on Yom Kippur or Shacharis on Shabbos.
Some people do, in fact, have the custom of saying Aleinu after Shacharis on Shabbos and repeating it after Musaf. Some chassididm also say Aleinu after Shacharis on Rosh Chodesh. These customs seem to accord with the opinion of the Arizal that Aleinu is an integral part of each tefillah.
It is possible, though, that the custom of not saying Aleinu after Shacharis on Shabbos is also in accordance with the Arizal. Since we daven Shacharis and Musaf together, perhaps they are considered one tefillah.
With two explanations – of the Bach and Arizal – in mind, we can now explain the machlokes regarding when to count Sefiras HaOmer, says the sefer Harirai Kedem. If Aleinu is part of Maariv, it should precede Sefiras HaOmer since we always perform more common mitzvot first – “tadir veshe’aino tadir, tadir kodem” – and Aleinu is part of the mitzvah of davening Ma’ariv. If, however, Aleinu was instituted as the last tefillah to be said prior to leaving shul, Sefiras HaOmer should be said first followed by Aleinu.
I would like to suggest that the machlokes regarding Aleinu also explains different customs regarding when to say Shir Shel Yom in Shacharis every day. Most people who daven Nusach Ashkenaz recite Aleinu followed by Shir Shel Yom (which seems to accord with the Arizal’s explanation). Those who daven Nusach Sephard, however, reverse the order (which seems to accord with the Bach’s explanation). Although it is Nusach Sephard – not Nusach Ashkenaz – that usually follows the Arizal, in this instance it seems that it is Nusach Ashkenaz that is in accordance with the Arizal’s opinion (that Aleinu is simply the conclusion of each tefillah as opposed to it being the every last thing a person must say before he leaves shul).