The MAPFLAT function is used to repeat a formula over a set of values. It assigns values specified by the first parameter to variable name, one at a time, and for each value, executes the formula specified by expression. Note that the second parameter, variable name, must be entered as a literal string (with quotes) while the variable used in expression is entered as a $variable. For instructions on creating variables, see Variables.
Syntax and Parameters: MAPFLAT( values, variable name, expression)
Notes:
- MAPFLAT and MAP are rarely used and typically only used for the cases described below.
- We recommend using MAPFLAT rather than MAP because MAPFLAT supports returning multiple items per 1st parameter while MAP returns only 1 item.
- MAPFLAT can result in your formulas taking longer to execute depending on the complexity and total number of values.
Dynamic data sources
The MAPFLAT and MAP function can be used to repeat a formula for a set of dynamic data source instances, where the variable specified in the MAPFLAT or MAP function is the variable used to create the dynamic data source. For more information, see Dynamic data sources.
The following example uses a Google Analytics dynamic data source that uses the variable, gaID, for the Profile ID:
https://www.googleapis.com/analytics/v3/data/ga?ids=ga:{props.gaID}&dimensions=ga:country&metrics=ga:sessions&start-date={date.add(-30).format()}&end-date={date.today}&max-results=10000
The above query returns Country in column A and Sessions in Column B.
The following MAP function returns the total number of sessions, SUM( SLICE( @B:B; ) )
, for each dynamic data source instance created by setting gaID to gaID1, gaID2, and gaID3:
MAP( ARRAY( "gaID1", "gaID2", "gaID3"), "gaID", SUM( SLICE( @B:B )))
The following MAPFLAT function returns the sessions for each country, SLICE( @B:B; )
, for each dynamic data source instance created by setting gaID to gaID1, gaID2, and gaID3:
MAPFLAT( ARRAY( "gaID1", "gaID2", "gaID3"), "gaID", SLICE( @B:B ))
You can also use dynamic data sources when you need to get more than one page of data. You can set up your data source to have a page or offset value as the dynamic property of the data source. See this article for more details.
JSON and XML xpath manipulation
In rare cases, based on the xpath structure of your data, you may need to use a MAPFLAT to loop through the JSON elements and get the data for each. There are many ways to work with JSON data that should be used first and these are outlined here.