Creating geolocation transformation scripts
This guide explains how to create a transformation script that will create a geolocation dimension.
Introduction
A geo map metric (such as Clicks or Impressions) on a map, showing where the metric took place. To display the location of a metric on a geo map, Adverity needs a dimension containing geolocation data.
displays aThis geolocation dimension is created by combining the values of latitude and longitude into a single value. This combination is achieved using a transformation script.
The transformation script you need to create contains two instructions:
-
addfieldx - In this example, this instruction combines the latitude and longitude values into a single value, geolocation.
-
convertx - In this example, this instruction converts geolocation into a decimal string, a format that Adverity can read to plot the locations on a geo map.
This guide outlines how to create a mapping table and apply it to a datastream using the map custom script. However, this option is considered an advanced approach as it allows for a lot of customization. For an easier way to apply mapping tables to your datastream, consider using the Match & Map or Unify Location Data transformation. For more information on these transformations, see the following:
Prerequisites
Before you read this reference, perform all of the following actions:
-
Ensure the data source from which you are collecting data contains the following location fields:
-
Latitude (required for the creation of the geolocation dimension)
-
Longitude (required for the creation of the geolocation dimension)
-
-
(Optional) To increase performance, ensure the latitude and longitude values are set to no more than two decimal places. This is achieved through a transformation script. If you wish to round the latitude and longitude values, ensure the rounding script is run before the geolocation-creation script. For more information see, Creating rounding transformation script.
-
Create a datastream to collect data from this data source. For more information, see Creating a datastream.
-
Find the names of the columns in the data extract that contain the latitude and longitude values.
Procedure
To create a transformation script that combines latitude and longitude into a single value that is also in the correct format, follow these steps:
-
Go to the Transformations page.
-
In the top right corner, click + Create transformation.
-
Click Custom Script.
-
To assign the transformation to individual datastreams of your choice, follow these steps:
-
In the Assign to section, select Individual datastreams.
-
In the drop-down menu, select the datastreams to assign the transformation to them.
-
-
To assign the transformation to all existing and new datastreams that belong to Datastream Types of your choice, follow these steps:
-
In the Assign to section, select Datastreams Types.
-
In the drop-down menu, select the Datastream Types to assign the transformation to all datastreams that belong to these types.
-
(Optional) In the Do not assign to these individual datastreams drop-down menu, select the individual datastreams that belong to the Datastream Types that you have selected, but you do not want to assign the transformation to them.
-
-
Click Next.
-
In Select Instruction, enter and select addfieldx.
-
In Fieldname, enter the value geolocation. This is the name of the new column that will be created using the transformation instruction.
-
In Python Expression, enter the following code. Replace the values of
latitude
andlongitude
with the corresponding latitude and longitude names from the data extract (For example, these names could belat
andlong
, orlt
andln
).{latitude} + ',' + {longitude}
-
Below the addfieldx instruction, click Add Instruction.
-
In Select Instruction, enter and select convertx.
-
In Field, select String from the drop-down field and enter geolocation into the text field.
-
In Python Expression, enter the following code to convert the geolocation dimension into the correct format.
{geolocation}.replace('0.0,0.0','')
-
Select the Fail on Error checkbox.
-
Click Next.
-
(Optional) In Transformation name, rename the transformation.
-
Click Create +.
As a result, the transformation script is created and can be applied to the relevant datastream to create the geolocation dimension.
Applying the transformation to a datastream
To apply the transformation to a datastream, follow these steps:
-
Go to the Datastreams page.
-
Click the datastream to which you want to apply the transformation.
-
In the Transformations section, click + Add transformation.
-
Click Assign Existing transformation.
-
To the right of the transformation you want to apply to the datastream, click + Assign
-
In the top right corner of the page, click Close to close the Assign transformations window.
As a result, the transformation script is added to the datastream. Click Fetch now to collect data in the datastream with the applied transformation script to combine latitude and longitude into a single value. Adverity places this single combined value in to the new column (geolocations) in the data extract.
Loading data extracts containing geolocations
To load the data extract that contains the geolocations dimension into Explore & Present, the datastream must contain Data Mapping. When setting up a Data Mapping, you may need to create a new target field for geolocations. For more information on creating this target field for the Data Mapping, see Mapping source fields.