-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_attribution_data.rb
137 lines (121 loc) · 3.55 KB
/
process_attribution_data.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
require "rubygems"
require "fastercsv"
require "attlib"
require "uri"
# Helper method to check if URI is valid
def uri?(string)
uri = URI.parse(string)
%w( http https ).include?(uri.scheme)
rescue URI::BadURIError
false
rescue URI::InvalidURIError
false
end
# Following is the list of fields pulled from each line of data
fields = [
"appId",
"platform",
"dt",
"tm",
"event",
"eventId",
"txnId",
"vTracker",
"vCollector",
"vEtl",
"userId",
"userIpaddress",
"userFingerprint",
"visitId",
"pageUrl",
"pageTitle",
"pageReferrer",
"mktSource",
"mktMedium",
"mktTerm",
"mktContent",
"mktCampaign",
"evCategory",
"evAction",
"evLabel",
"evProperty",
"evValue",
"trOrderid",
"trAffiliation",
"trTotal",
"trTax",
"trShipping",
"trCity",
"trState",
"trCountry",
"tiOrderid",
"tiSku",
"tiName",
"tiCategory",
"tiPrice",
"tiQuantity",
"useragent",
"brName",
"brFamily",
"brVersion",
"brType",
"brRenderengine",
"brLang",
"brFeaturesPdf",
"brFeaturesFlash",
"brFeaturesJava",
"brFeaturesDirector",
"brFeaturesQuicktime",
"brFeatursRealplayer",
"brFeaturesWindowsmedia",
"brFeaturesGears",
"brFeaturesSilverlight",
"brCookies",
"brColordepth",
"osName",
"osFamily",
"osManufacturer",
"osName",
"dvceType",
"dvceIsmobile",
"dvceScreenwidth",
"dvceScreenHeight"]
# Create a new output file
FasterCSV.open("output.csv", "w", { :col_sep => "\t" }) do |csv|
# Open the raw tsv file, and process each line one at a time
FasterCSV.foreach("events_arv.txt", { :col_sep => "\t" }) do |row|
# Create a Hash of all the data points in the line by zipping with fields array
eventData = Hash[fields.zip(row)]
unless eventData["pageReferrer"].nil?
puts("page_referrer = " + eventData["pageReferrer"])
end
# Second check to see if the marketing fields are set AND if page_referrer is a valid URL...
if (eventData["mktMedium"].nil? and
eventData["mktSource"].nil? and
eventData["mktTerm"].nil? and
eventData["mktContent"].nil? and
eventData["mktCampaign"].nil? and
uri?(eventData["pageReferrer"])
) then
referrer = Referrer.new(eventData["pageReferrer"])
if referrer.is_search_engine?
# set mkt_medium = "organic", mkt_source = searceventData engine name, mkt_term = searceventData engine terms
eventData['mktMedium'] = "organic"
eventData['mktSource'] = referrer.is_search_engine?
eventData['mktTerm'] = referrer.keywords
elsif eventData["pageReferrer"].include? "web-analyse.no"
# do not do anything - referrer is internal, so don't set any mkt values
else
# if not search engine, set `mktMedium` to `referrer` and `mktSource` to referrer domain
eventData['mktMedium'] = "referrer"
eventData['mktSource'] = URI(eventData['pageReferrer']).host
# set mkt_medium = "referrer", mkt_source = domain(pageReferrer)
end
end
puts "pageReferrer = " + (eventData["pageReferrer"]|| "") + " mktMedium = " + (eventData["mktMedium"]|| "") +
" mktSource" + (eventData["mktSource"]|| "")
# Now write a new line of data to the output.csv file.
# Note - we use the original field array to maintain order of fields in output
csv << fields.map { |f| eventData[f] }
end
end