MediaWiki:Common.js

From Riverview Legal Group
Revision as of 04:55, 21 February 2025 by Sharvey (talk | contribs)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
console.log("Common.js is loading correctly!");

mw.loader.using(['jquery', 'mediawiki.user'], function () {
    $(document).ready(function () {
        var popupShown = sessionStorage.getItem("popupShown");
        var popupTimeout = 120000; // 2 minutes in milliseconds
        var timeoutTriggered = false;

        // Function to send analytics event
        function sendAnalyticsEvent() {
            if (typeof gtag === "function") {
                gtag('event', 'popup_shown', {
                    'event_category': 'Engagement',
                    'event_label': 'Legal Consultation Popup',
                    'value': 1
                });
            } else {
                console.warn("Google Analytics not detected.");
            }
        }

        // Function to display the popup
        function showPopup() {
            var popup = $('<div id="popup-overlay">' +
                '<div id="popup-box">' +
                '<h2>Free Legal Consultation <br> Riverview Legal Group (Ontario)</h2>' +
                '<p>Our primary focus is the Ontario Landlord and Tenant Board.</p>' +
                '<p>Call us for help at (888) 655-1076</p>' +
                '<a href="https://riverview.legal/about-us/" target="_blank" class="popup-button">Book a Consultation</a>' +
                '<span id="popup-close">×</span>' +
                '</div>' +
                '</div>');

            $("body").append(popup);
            sessionStorage.setItem("popupShown", "true");

            // Send Google Analytics event
            sendAnalyticsEvent();

            // Close popup on click
            $("#popup-close").click(function () {
                $("#popup-overlay").fadeOut();
            });

            // Remove scroll listener after triggering
            $(document).off("scroll", scrollHandler);
        }

        // Function to handle scroll and show popup
        function scrollHandler() {
            if (!popupShown || timeoutTriggered) {
                showPopup();
            }
        }

        // Check if the user is logged in
        var loggedInUser = mw.config.get('wgUserName');
        console.log("Logged-in User:", loggedInUser);

        // Reset popup flag on new page load
        sessionStorage.removeItem("popupShown");

        // Trigger popup if user is logged out or timeout triggered
        if (!loggedInUser) {
            console.log("User is logged out, showing popup.");
            $(document).one("scroll", scrollHandler);
        }

        // Set timeout to allow popup after 2 minutes (if user is not logged in)
        if (!loggedInUser) {
            setTimeout(function () {
                timeoutTriggered = true;
                $(document).one("scroll", scrollHandler);
            }, popupTimeout);
        }
    });
});

// Add CSS styles for the popup
mw.util.addCSS(
    "#popup-overlay {" +
    "position: fixed;" +
    "top: 0;" +
    "left: 0;" +
    "width: 100%;" +
    "height: 100%;" +
    "background: rgba(0, 0, 0, 0.6);" +
    "display: flex;" +
    "justify-content: center;" +
    "align-items: center;" +
    "z-index: 9999;" +
    "}" +
    "#popup-box {" +
    "background: white;" +
    "padding: 20px;" +
    "border-radius: 10px;" +
    "text-align: center;" +
    "width: 300px;" +
    "box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);" +
    "position: relative;" +
    "}" +
    ".popup-button {" +
    "display: inline-block;" +
    "background: #007bff;" +
    "color: white;" +
    "padding: 10px;" +
    "text-decoration: none;" +
    "border-radius: 5px;" +
    "margin-top: 10px;" +
    "}" +
    "#popup-close {" +
    "position: absolute;" +
    "top: 10px;" +
    "right: 15px;" +
    "cursor: pointer;" +
    "font-size: 20px;" +
    "}"
);