We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3677
    • 130 Posts
    I have everything setup in Revo 2.1.3 to use the modx session handler. I'd need a unique ID that I can pass along to our backend during form submission and also to Google Analytics in order to be able to correlate data.

    What's the proper snippet syntax to retrieve the unique ID that is associated with a modx managed session?

    Thanks,
    -Brian
      • 3677
      • 130 Posts
      Well, I couldn't figure out how to get the value from the modx db, so I tried to grab it directly from the cookie that is created.
      /***************************************************************
       * Save SessionID.
       ***************************************************************/
          if (isset($_COOKIE["PHPSESSID"]))
          $sID = $_COOKIE["PHPSESSID"];
          $_SESSION['sID'] = $sID;


      It works, but I am noticing that with a clean browser, the value is not set on the first page load. If I refresh the page then I get the cookie value. I'm calling the snippet in the head of the html, since this is where I'll need to be able to send the value to GA.

      It's as if the cookie is set but the value is empty when this fires the very first time. Any suggestions?


        • 9207 ☆ A M B ☆
        • 2,475 Posts
        Did you try using session_id()? And/or session_is_registered()?

        http://php.net/manual/en/ref.session.php

        Like maybe

        if (!session_is_registered()) {
           $sID = session_id();
        }


        The id eventually gets stored in the user's cookie anyway, so the method you outlined seems legit to me.
          • 22303 MODX Staff
          • 10,725 Posts
          The session_id changes anytime a user logs in. This is not a unique identifier for a user. You would be much better off using a hash of the user id or some other persistent value.
            • 3677
            • 130 Posts
            Hmm....Let me clarify what I am trying to do...


            • I'd like to generate a unique identifier that can be associated with each pageview in Google Analytics including the visitors initial landing page.
            • This unique ID would be passed along during GA async page tracking as a custom variable.
            • Every pageview from that visitor in GA reporting would then be tagged with this custom var containing the unique id.

            Most of our visitors are anonymous, and are not logging into modx at all.
            The GA tracking code fires in the html head, so the tricky part is that the unique ID would have to be available prior to processing the head of the visitors landing page.

            I had envisioned using a Session Var to store this unique ID and retrieve it in the GA code, but that would of course require that a Session is established in time to store the var and retrieve it on the very first page load.

            It doesn't have to be the actual session ID, it just has to be unique.
            I am already running a mutliple context gateway plugin on the site that detects the hostname and redirects to the appropriate site context's home page. It seemed like this would be the place to generate the id, but I am not clear on when the sessions are actually established in modx with the default db session management.

            Thanks,



              • 22303 MODX Staff
              • 10,725 Posts
              Are these sub-domain contexts or what is the difference between the Contexts?
                • 9207 ☆ A M B ☆
                • 2,475 Posts
                I wrote a script a while ago that did something like this for similar purposes: it set a cookie for each user -- if the cookie already existed, the GA code wasn't displayed at all. The result (give or take errors from users clearing or disabling cookies) was that each user tracked a single hit in GA.
                  • 3677
                  • 130 Posts
                  og,

                  The contexts are all unique domains. The reason for the many contexts is that they all share common elements, i.e. templates, etc... Think of it as cookie-cutter sites. Look very similar but they have their own resources.

                  Everett,

                  At what point were you executing your script? Is a non-logged in visit just an "anonymous" user?

                    • 9207 ☆ A M B ☆
                    • 2,475 Posts
                    My script was written as a Snippet placed inside page templates. It conditionally displayed HTML text based on the status of a user's $_COOKIE. It allowed some text to *always* be displayed, other text could be displayed once per day, and other text could be displayed once per month.

                    The text we conditionally displayed was primarily Piwik and Open Web Analytics tracking code ... we could have used it to do Google Analytics code too. I think all that it did really was help simplify the views in the various reporting softwares. That stuff is all about easy reporting, so even though they probably could have boiled down the raw data to show unique visitors in the same way, this let them add specific channels and reports dedicated to the unique daily and monthly visitors.

                    I probably could clean it up and release it as an extra if you're interested.
                      • 22303 MODX Staff
                      • 10,725 Posts
                      Quote from: bmilton at Jan 02, 2012, 02:21 PM
                      The contexts are all unique domains. The reason for the many contexts is that they all share common elements, i.e. templates, etc...
                      Each domain will maintain a separate PHP session; all PHP sessions are limited to a single domain (or you can optionally configure them to work across all subdomains of a particular domain). You cannot share session data across domains.

                      Quote from: bmilton at Jan 02, 2012, 02:21 PM
                      At what point were you executing your script? Is a non-logged in visit just an "anonymous" user?
                      Anytime a non-logged-in user visits a MODX Resource, they are indeed visiting as an anonymous user. MODX starts the session as soon as the Context configuration is loaded (during the $modx->initialize() method), so you can set session variables within a specific domain for the anonymous users.