JoshHouse
Occasional Observer

Snippet Code Issues

I have a snippet I am trying to ad to a landing page but am having difficulty getting the code to work properly. What am I missing? Please advise.

Note: The phone numbers used are dummy numbers for practice purposes. 

// Parse the URL parameter
function getParam(source, url) {
if (!url) url = window.location.href;
// url = window.location.href;
source = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + source + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
// Assign the referral source to the UTM code
var sourceName = getParam('utm_source');
$(document).ready(function() {
// Check the UTM code and assign the correct unique phone number per source
if (sourceName == 'google') {
$('#yourDiv').html('(415)-111-0000');
}
else if (sourceName == 'facebook') {
$('#yourDiv').html('(415)-222-0000');
}
else if (sourceName == 'newsletter') {
$('#yourDiv').html('(415)-333-0000');
}
else {
$('#dki').html('<p><b>normal number - (415)-000-0000</b><p>');
}
});

 

0 Kudos
4 Replies
MarcusEdwards
Crownpeak (Retired)

A couple of things:

  1. What is the difficulty you're having getting the code to work? Does it not do what you expected or do you get Javascript errors in the console?
  2. Does this code work outside of the snippet? If you put this on a standalone page, something like a jsfiddle for example, does it do what you're expecting?
0 Kudos

1. What is the difficulty you're having getting the code to work? Does it not do what you expected or do you get Javascript errors in the console?

The code is doing what I am expecting it to do. To be 100% transparent I am not sure I am even setting this up correctly in the first place. I am trying to create a dynamic number insertion code for our marketing campaigns. I worked with Twilio and used the script they suggested.

Do I need to have the phone number placed in the "Content" as well or does the code I am using place it there for me? 

2. Does this code work outside of the snippet? If you put this on a standalone page, something like a jsfiddle for example, does it do what you're expecting?

I tried to use the code on CodePen and then again on jsfiddle. When I ran the code it "nothing happened" I am assuming that this is because this is based on the "source" in the UTM and when I run the code there is no UTM that is being referenced. 

I apologize for my lack of knowledge I have a general idea as to what I am trying accomplish, but this is more complex than my current skillset. 

 

0 Kudos

After some research I am trying a new code: 

 

<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
	// Parse the URL parameter
	function getParameterByName(name, url) {
	    if (!url) url = window.location.href;
	    name = name.replace(/[\[\]]/g, "\\$&");
	    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
	        results = regex.exec(url);
	    if (!results) return null;
	    if (!results[2]) return '';
	    return decodeURIComponent(results[2].replace(/\+/g, " "));
	}
	// Give the parameter a variable name
	var dynamicContent = getParameterByName('utm_source');
 
	 $(document).ready(function() {
 
		// Check if the URL parameter is google
		if (dynamicContent == 'google') {
			$('(616) 685-74100').show();
		} 
		// Check if the URL parameter is facebook
		else if (dynamicContent == 'facebook') {
			$('(616) 685-7894').show();
				} 
		// Check if the URL parameter is empty or not defined, display default content
		else {
			$('(616) 685-7764').show();
		}
	});
</script>

 

 When I place the snippet onto content editor and put in either the link for the utm_source to equal "google" or "facebook" nothing seems to happen. I am trying to get the phone number to change depending on the source. Any help is appreciated. 

0 Kudos

.

0 Kudos