Kentico v9 how can i detect when a user is in CMS

2019-09-05 18:10发布

问题:

I'm in the process of writing some JS for custom event tracking in Google Analytics. I want to avoid event tracking while the site is being updated.

I do have access to change the code behind of my main .Master page so add an attribute to the body that than i can check against.

The Javascript webpart may not be an option, since this is global code, rather template specific.

回答1:

I'd still consider web part that could be placed on the master page, but rendered only if this is live site. Here is similar question.



回答2:

If you're using Kentico properly, you'll have a master page which all other pages are inheriting from so the master page template will be "global" and it is a solution.

Not only can you use the macro in the link Roman mentioned (which really isn't needed because the javascript webpart only renders on the "live site" anyway) but you can go one step further and have it only display on the production site with a visibility macro like this if your domain contains "staging":

{% !RootDocument.AbsoluteURL.Contains("staging.") @%}



回答3:

Here is non-webpart way. Make sure your script block is NOT in the head tag. Needs to be in the body tag or you will get a .Net exception (not Kentico related).

Look up the list of valid enums. LiveSite, EditLive, Preview are a few common ones I use.

<script type="text/javascript">
    $(document).ready(function () {

        <%if(PageManager.ViewMode == CMS.PortalEngine.ViewModeEnum.LiveSite) {%>

             $('#my-control').hide();

        <%}%>  
    }); 

</script>


标签: kentico