Pascal Lindelauf
Pascal Lindelauf

Reputation: 4870

How to add Google Analytics cross-domain linker parameters to an URL with javascript (using GTM)?

On our site, we have a couple of buttons that will take you to a different domain but not before we've composed the actual URL with appropriate query parameters in Javascript. What is the best way to add Google Analytic's linker parameter to this URL? We're using the universal script injected by Google Tag Manager.

Upvotes: 1

Views: 1174

Answers (1)

Pascal Lindelauf
Pascal Lindelauf

Reputation: 4870

Ah... found it. Documentation suggests:

ga(function(tracker) { var linkerParam = tracker.get('linkerParam'); })

But the tracker is undefined in my case.

I found the solution in this blog post:

if (typeof(window["ga"]) !== "undefined") {
  var firstTracker = ga.getAll()[0];
  if (firstTracker) { return firstTracker.get("linkerParam"); }
}

But... on further investigation, I found the root cause: using GTM means that there are potentially multiple trackers active on a page. Hence the single ga-function won't work. I haven't found the exact GTM-compatible way of doing this, but I'll stick with this workaround for now.

Upvotes: 1

Related Questions