def build_query_string(args,profile)
      query_params  = args.clone
      ga_start_date = query_params.delete(:start_date)
      ga_end_date   = query_params.delete(:end_date)
      ga_dimensions = query_params.delete(:dimensions)
      ga_metrics    = query_params.delete(:metrics)
      ga_sort       = query_params.delete(:sort)
      ga_filters    = query_params.delete(:filters)
      
      output = "ids=ga:#{profile}&start-date=#{ga_start_date}&end-date=#{ga_end_date}"
      unless ga_dimensions.nil? || ga_dimensions.empty?
        output += '&dimensions=' + ga_dimensions.collect do |dimension|
          "ga:#{dimension}"
        end.join(',')
      end
      unless ga_metrics.nil? || ga_metrics.empty?
        output += '&metrics=' + ga_metrics.collect do |metric|
          "ga:#{metric}"
        end.join(',')
      end
      unless ga_sort.nil? || ga_sort.empty?
        output += '&sort=' + Array(ga_sort).collect do |sort|
          sort[0..0] == '-' ? "-ga:#{sort[1..-1]}" : "ga:#{sort}"  # if the first character is a dash, move it before the ga:
        end.join(',')
      end
      
      # TODO: update so that in regular expression filters (=~ and !~), any initial special characters in the regular expression aren't also picked up as part of the operator (doesn't cause a problem, but just feels dirty)
      unless args[:filters].empty?    # filters are a little more complicated because they can have all kinds of modifiers
        output += '&filters=' + args[:filters].collect do |filter|
          match, name, operator, expression = *filter.match(/^(\w*)\s*([=!<>~@]*)\s*(.*)$/)           # splat the resulting Match object to pull out the parts automatically
          unless name.empty? || operator.empty? || expression.empty?                      # make sure they all contain something
            "ga:#{name}#{CGI::escape(operator.gsub(/ /,''))}#{CGI::escape(expression)}"   # remove any whitespace from the operator before output
          else
            raise GatticaError::InvalidFilter, "The filter '#{filter}' is invalid. Filters should look like 'browser == Firefox' or 'browser==Firefox'"
          end
        end.join(';')
      end
 
      query_params.inject(output) {|m,(key,value)| m << "&#{key}=#{value}"}
 
      return output
    end
def build_query_string(args,profile)
      query_params  = args.clone
      ga_start_date = query_params.delete(:start_date)
      ga_end_date   = query_params.delete(:end_date)
      ga_dimensions = query_params.delete(:dimensions)
      ga_metrics    = query_params.delete(:metrics)
      ga_sort       = query_params.delete(:sort)
      ga_filters    = query_params.delete(:filters)
      
      output = "ids=ga:#{profile}&start-date=#{ga_start_date}&end-date=#{ga_end_date}"
      unless ga_dimensions.nil? || ga_dimensions.empty?
        output += '&dimensions=' + ga_dimensions.collect do |dimension|
          "ga:#{dimension}"
        end.join(',')
      end
      unless ga_metrics.nil? || ga_metrics.empty?
        output += '&metrics=' + ga_metrics.collect do |metric|
          "ga:#{metric}"
        end.join(',')
      end
      unless ga_sort.nil? || ga_sort.empty?
        output += '&sort=' + Array(ga_sort).collect do |sort|
          sort[0..0] == '-' ? "-ga:#{sort[1..-1]}" : "ga:#{sort}"  # if the first character is a dash, move it before the ga:
        end.join(',')
      end
      
      # TODO: update so that in regular expression filters (=~ and !~), any initial special characters in the regular expression aren't also picked up as part of the operator (doesn't cause a problem, but just feels dirty)
      unless args[:filters].empty?    # filters are a little more complicated because they can have all kinds of modifiers
        output += '&filters=' + args[:filters].collect do |filter|
          match, name, operator, expression = *filter.match(/^(\w*)\s*([=!<>~@]*)\s*(.*)$/)           # splat the resulting Match object to pull out the parts automatically
          unless name.empty? || operator.empty? || expression.empty?                      # make sure they all contain something
            "ga:#{name}#{CGI::escape(operator.gsub(/ /,''))}#{CGI::escape(expression)}"   # remove any whitespace from the operator before output
          else
            raise GatticaError::InvalidFilter, "The filter '#{filter}' is invalid. Filters should look like 'browser == Firefox' or 'browser==Firefox'"
          end
        end.join(';')
      end
 
      query_params.inject(output) {|m,(key,value)| m << "&#{key}=#{value}"}
 
      return output
    end
Creates a valid query string for GA