Nick Heer
Nick Heer

Reputation: 1573

Can I set a disqus_identifier without losing old comments? Dynamically?

End result: I'm trying to fix an issue with Disqus reading links with UTM URL parameters as separate pages from the clean URL.

Back story: When Disqus was originally installed on this site on HubSpot, no options were set other than the forum shortname. When readers visit blog posts directly or via a clean URL with no tracking parameters, they'll see the correct Disqus comments thread. But if they visit the post with a "dirty" URL with a bunch of tracking parameters, such as one of the ones sent with our email subscription, they'll see a Disqus comment thread associated with that specific URL.

Obviously, we want people who visit via the email link to be able to see the same comments thread as the people who visit via our own site.

The way I'm thinking of solving this is to use the xxxxxxxxx part of the hs-content-id-xxxxxxxxx class in the <body> to set the disqus_identifier:

var hsbody = document.body.classList;
var hsid = hsbody.item(0);
var disqus_identifier = hsid.replace("hs-content-id-", "");

This raises some questions and concerns:

  1. ~~Is it okay to set the identifier dynamically, in this way? Or, more to the point, is it high-test stupid to do this?~~ Edit: Disqus actually recommends setting the identifier dynamically, so I'm not entirely stupid here.
  2. If I set this globally, will old threads now have an identifier, and will that interfere with existing comments?
  3. Is there a way easier way to do this?
  4. I'm just thinking through this more while writing this and I've realized that I could probably just use the canonical link as the disqus_identifier. Thoughts?

Limitations: I can't disable tracking codes for the emails (though that would be the easiest way to do this). We don't have a dev site, so this is all live, so I'd rather not do too much guesswork here (though I can obviously revert easily).

Upvotes: 1

Views: 129

Answers (1)

Nick Heer
Nick Heer

Reputation: 1573

Since the disqus_identifier is set to the page URL if it's undefined, I just set it to the page’s canonical URL. I can’t lose!

var canonical = "";
        var links = document.getElementsByTagName("link");
            for (var i = 0; i < links.length; i ++) {
                if (links[i].getAttribute("rel") === "canonical") {
                    canonical = links[i].getAttribute("href");
                }
            }
        var disqus_identifier = canonical;

Yeah, it’s pretty gross. If you’d like to refine it, please be my guest. Seems to work, though.

Upvotes: 1

Related Questions