Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have the following URL assigned to a variable in a webpage using JavaScript. The code snippet is below.

JavaScript
googleURL = 'https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A76546294&dimensions='+'ga%3Asource&metrics=ga%3Ausers&sort=-ga%3Ausers&start-date=2014-04-05&end-date=2014-04-23&max-results=10';


The startdate & enddate values are static. Now I want that perticualr dates to be dynamic & the dates will be get from a HTML form & I already have that done. Those values are assigned into 2 different JS variables already. But I add those variables to the URL as below.

JavaScript
googleURL = 'https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A76546294&dimensions='+'ga%3Asource&metrics=ga%3Ausers&sort=-ga%3Ausers&start-date=strStartDate&end-date=strEndDate&max-results=10';


Please look at start-date and end-date, there you can find the JS variable names that I have replaced with static dates.

When I alterted the edited googleURL, it displays as at the below.

https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A76546294&dimensions=ga%3Asource&metrics=ga%3Ausers&sort=-ga%3Ausers&start-date=strStartDate&end-date=strEndDate&max-results=10

I think the variable values are not assigning to or replacing the specified variables. The code snippet I'm working is at the below.

JavaScript
var strStartDate;
var strEndDate;
var googleURL;

//The following JS function will pass the values from the form to the URL.
function passVariable() {

  strStartDate = document.getElementById("from_date").value;
  strEndDate = document.getElementById("to_date").value;

  alert(strStartDate);
  alert(strEndDate);
  //alert(googleURL);
  document.write(googleURL);

}

 googleURL = 'https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A76546294&dimensions='+'ga%3Asource&metrics=ga%3Ausers&sort=-ga%3Ausers&start-date=strStartDate&end-date=strEndDate&max-results=10';

 passVariable();


I'm using JavaScript. could someone helpme to solve this matter?

Thanks & regards,
Chiranthaka
Posted

1 solution

you can done like this

C#
var strStartDate;
var strEndDate;
var googleURL;

//The following JS function will pass the values from the form to the URL.
function passVariable() {

  strStartDate = document.getElementById("from_date").value;
  strEndDate = document.getElementById("to_date").value;

  alert(strStartDate);
  alert(strEndDate);

googleURL = 'https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A76546294&dimensions='+'ga%3Asource&metrics=ga%3Ausers&sort=-ga%3Ausers&start-date=' + strStartDate + '&end-date=' + strEndDate + '&max-results=10';

  document.write(googleURL);

}
 passVariable();
 
Share this answer
 
v3
Comments
Chiranthaka Sampath 16-Jul-14 5:47am    
Now its saying undefined for the values. I cannot understand what the problem is. The result is at the below.

https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A76546294&dimensions=ga%3Asource&metrics=ga%3Ausers&sort=-ga%3Ausers&start-date=undefined&end-date=undefined&max-results=10
ashok rathod 16-Jul-14 5:52am    
move this google URL statement just before you are doing document.write(). It will solve your purpose. Its actually sequencing issue. please see the updated solution.
Chiranthaka Sampath 16-Jul-14 6:10am    
Ok pal it worked. The issue actually is a sequencing issue. Thanks for the help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900