• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Update 800 hyperlinks with UTM tracking

Explorer ,
Mar 03, 2022 Mar 03, 2022

Copy link to clipboard

Copied

My client would like to add tracking to all their hyperlinks across 7 files, between 800-2200 SKUs each. This is not a batch find and replace situation, UTM was never used up until this new request,  so I don't think IDML option will work.

 

Is there a way to batch add the UTM tracking to all hyperlinks?

TOPICS
How to , Scripting

Views

916

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 03, 2022 Mar 03, 2022

Assuming the UTM needs to be appended to the end of all existing Hyperlink URL Destinations in the document: 

var hlds = app.activeDocument.hyperlinkURLDestinations;
var utm = "?utm=blahblah";
for (var i = 0; i < hlds.length; i++) {
    hlds[i].destinationURL += utm; 
}

 

Votes

Translate

Translate
Community Expert ,
Mar 03, 2022 Mar 03, 2022

Copy link to clipboard

Copied

Assuming the UTM needs to be appended to the end of all existing Hyperlink URL Destinations in the document: 

var hlds = app.activeDocument.hyperlinkURLDestinations;
var utm = "?utm=blahblah";
for (var i = 0; i < hlds.length; i++) {
    hlds[i].destinationURL += utm; 
}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 03, 2022 Mar 03, 2022

Copy link to clipboard

Copied

Wow - Brian - brillant. Haven't worked with scripts very much. What/where exactly am I using this beautiful set of instructions?

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 03, 2022 Mar 03, 2022

Copy link to clipboard

Copied

The above code is a jsx script. This explains how to install and use it. You'll need to run it on an open document (won't do all docs at once--that costs extra 😉 )

 

https://creativepro.com/how-to-install-a-script-in-indesign-that-you-found-in-a-forum-or-blog-post/

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 03, 2022 Mar 03, 2022

Copy link to clipboard

Copied

Many thanks kind sir!

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 03, 2022 Mar 03, 2022

Copy link to clipboard

Copied

Just had to fix a spelling mistake to make this script work. Thank you so very much!

Would this script also work to find and replace if they change the UTM info in the future?

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 03, 2022 Mar 03, 2022

Copy link to clipboard

Copied

Sure, just change this line: 

hlds[i].destinationURL += utm; 

To this: 

hlds[i].destinationURL = hlds[i].destinationURL.replace("?=oldutm", "?=newutm");

Keep the double quotes 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 03, 2022 Mar 03, 2022

Copy link to clipboard

Copied

Once Again - thank you very much!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 15, 2022 Nov 15, 2022

Copy link to clipboard

Copied

Thanks brian for making this.

 

I have updated the code a bit so the if your url has a search queery the code will change your ? to an &.

 

var utm = "utm=blahblah";

var hlds = app.activeDocument.hyperlinkURLDestinations;
var utmsearch = "&" + utm;
var utmdefault = "?" + utm;

for (var i = 0; i < hlds.length; i++) {
	var result = hlds.item(i).destinationURL.includes("?");
	if (result) {
		hlds.item(i).destinationURL += utmsearch; 
	} else {
		hlds.item(i).destinationURL += utmdefault;
	}
}
  1. Don't included the ? when replacing the UTM code
  2. save as a .idjs file

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 17, 2022 Nov 17, 2022

Copy link to clipboard

Copied

Im trying to run a similar script and am getting an error -wondering if anyone can help??

The code adds UTM tracking but also uses an if/then to check if there are more than one & in the code.

When I run this in InDesign I get an error I have attached as a screenshot.

 

Here is my code and error message - I believe the error is on the line shown in red?

 

var utm = "utm_source=publications&utm_medium=sale-finder&utm_campaign=12-days-of-xmas";

var hlds = app.activeDocument.hyperlinkURLDestinations;
var utmsearch = "&" + utm;
var utmdefault = "?" + utm;

for (var i = 0; i < hlds.length; i++) {
var result = hlds.item(i).destinationURL.includes("?");
if (result) {
hlds.item(i).destinationURL += utmsearch;
} else {
hlds.item(i).destinationURL += utmdefault;
}
}

 

InDesign Error.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 17, 2022 Nov 17, 2022

Copy link to clipboard

Copied

The person who responded to my code wrote it in the new UXP scripting language (hence the idjs). Extendscript doesn't include ".include" as a string method. Try this instead. 

 

var utm = "utm_source=publications&utm_medium=sale-finder&utm_campaign=12-days-of-xmas";

var hlds = app.activeDocument.hyperlinkURLDestinations;
var utmsearch = "&" + utm;
var utmdefault = "?" + utm;

for (var i = 0; i < hlds.length; i++) {
var result = hlds.item(i).destinationURL.indexOf("?") >= 0;
if (result) {
hlds.item(i).destinationURL += utmsearch;
} else {
hlds.item(i).destinationURL += utmdefault;
}
}

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 17, 2022 Nov 17, 2022

Copy link to clipboard

Copied

Wow I think that works.

 

Thanks sooooooooooooooo much Brian

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 21, 2022 Nov 21, 2022

Copy link to clipboard

Copied

LATEST

Thanks for fixing my error

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines